Skip to content

Commit

Permalink
Convert to kt
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 authored and AndyScherzinger committed Nov 13, 2023
1 parent 8c1d97d commit 9e455bf
Showing 1 changed file with 117 additions and 130 deletions.
247 changes: 117 additions & 130 deletions app/src/main/java/com/owncloud/android/ui/activity/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,135 +18,128 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.owncloud.android.ui.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;

import com.nextcloud.client.account.User;
import com.nextcloud.client.preferences.DarkMode;
import com.nextcloud.java.util.Optional;
import com.owncloud.android.R;
import com.owncloud.android.databinding.ShareActivityBinding;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
import com.owncloud.android.lib.resources.files.model.RemoteFile;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.operations.GetSharesForFileOperation;
import com.owncloud.android.ui.fragment.FileDetailSharingFragment;
import com.owncloud.android.ui.fragment.FileDetailsSharingProcessFragment;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.MimeTypeUtil;

import javax.inject.Inject;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
package com.owncloud.android.ui.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.nextcloud.client.account.User
import com.owncloud.android.R
import com.owncloud.android.databinding.ShareActivityBinding
import com.owncloud.android.datamodel.SyncedFolderProvider
import com.owncloud.android.datamodel.ThumbnailsCacheManager
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.lib.resources.shares.ShareType
import com.owncloud.android.operations.GetSharesForFileOperation
import com.owncloud.android.ui.fragment.FileDetailSharingFragment
import com.owncloud.android.ui.fragment.FileDetailsSharingProcessFragment
import com.owncloud.android.ui.fragment.FileDetailsSharingProcessFragment.Companion.newInstance
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.MimeTypeUtil
import javax.inject.Inject

/**
* Activity for sharing files.
*/
public class ShareActivity extends FileActivity {

private static final String TAG = ShareActivity.class.getSimpleName();

static final String TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT";
class ShareActivity : FileActivity() {

@JvmField
@Inject
SyncedFolderProvider syncedFolderProvider;
var syncedFolderProvider: SyncedFolderProvider? = null

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

ShareActivityBinding binding = ShareActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
val binding = ShareActivityBinding.inflate(layoutInflater)
setContentView(binding.root)

OCFile file = getFile();
Optional<User> optionalUser = getUser();
if (!optionalUser.isPresent()) {
finish();
return;
val optionalUser = user
if (!optionalUser.isPresent) {
finish()
return
}

// Icon
if (file.isFolder()) {
boolean isAutoUploadFolder = SyncedFolderProvider.isAutoUploadFolder(syncedFolderProvider, file, optionalUser.get());
setupIcon(binding, optionalUser.get())
setupFileInfo(binding)
readFile(binding, optionalUser.get())

if (savedInstanceState == null) {
addShareFragment(optionalUser.get())
}
}

Integer overlayIconId = file.getFileOverlayIconId(isAutoUploadFolder);
LayerDrawable drawable = MimeTypeUtil.getFileIcon(preferences.isDarkModeEnabled(), overlayIconId, this, viewThemeUtils);
binding.shareFileIcon.setImageDrawable(drawable);
private fun setupIcon(binding: ShareActivityBinding, user: User) {
if (file.isFolder) {
val isAutoUploadFolder =
SyncedFolderProvider.isAutoUploadFolder(syncedFolderProvider, file, user)
val overlayIconId = file.getFileOverlayIconId(isAutoUploadFolder)
val drawable = MimeTypeUtil.getFileIcon(preferences.isDarkModeEnabled, overlayIconId, this, viewThemeUtils)
binding.shareFileIcon.setImageDrawable(drawable)
} else {
binding.shareFileIcon.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimeType(),
file.getFileName(),
this,
viewThemeUtils));
binding.shareFileIcon.setImageDrawable(
MimeTypeUtil.getFileTypeIcon(
file.mimeType,
file.fileName,
this,
viewThemeUtils
)
)
if (MimeTypeUtil.isImage(file)) {
String remoteId = String.valueOf(file.getRemoteId());
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId);
val remoteId = file.remoteId.toString()
val thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(remoteId)
if (thumbnail != null) {
binding.shareFileIcon.setImageBitmap(thumbnail);
binding.shareFileIcon.setImageBitmap(thumbnail)
}
}
}
}

// Name
binding.shareFileName.setText(getResources().getString(R.string.share_file, file.getFileName()));

viewThemeUtils.platform.colorViewBackground(binding.shareHeaderDivider);

// Size
binding.shareFileSize.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));

Activity activity = this;
new Thread(() -> {
RemoteOperationResult result = new ReadFileRemoteOperation(getFile().getRemotePath())
.execute(optionalUser.get(),
activity);
private fun setupFileInfo(binding: ShareActivityBinding) {
binding.shareFileName.text = resources.getString(R.string.share_file, file.fileName)
viewThemeUtils.platform.colorViewBackground(binding.shareHeaderDivider)

if (result.isSuccess()) {
RemoteFile remoteFile = (RemoteFile) result.getData().get(0);
long length = remoteFile.getLength();
binding.shareFileSize.text = DisplayUtils.bytesToHumanReadable(file.fileLength)
}

getFile().setFileLength(length);
runOnUiThread(() -> binding.shareFileSize.setText(DisplayUtils.bytesToHumanReadable(length)));
private fun readFile(binding: ShareActivityBinding, user: User) {
Thread {
val result = ReadFileRemoteOperation(file.remotePath).execute(user, this)
if (result.isSuccess) {
val remoteFile = result.data[0] as RemoteFile
val length = remoteFile.length
file.fileLength = length
runOnUiThread { binding.shareFileSize.text = DisplayUtils.bytesToHumanReadable(length) }
}
}).start();
}.start()
}

if (savedInstanceState == null) {
// Add Share fragment on first creation
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = FileDetailSharingFragment.newInstance(getFile(), optionalUser.get());
ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT);
ft.commit();
}
private fun addShareFragment(user: User) {
val ft = supportFragmentManager.beginTransaction()
val fragment: Fragment = FileDetailSharingFragment.newInstance(file, user)
ft.replace(R.id.share_fragment_container, fragment, TAG_SHARE_FRAGMENT)
ft.commit()
}

@Override
protected void onStart() {
super.onStart();
override fun onStart() {
super.onStart()

// Load data into the list
Log_OC.d(TAG, "Refreshing lists on account set");
refreshSharesFromStorageManager();
Log_OC.d(TAG, "Refreshing lists on account set")
refreshSharesFromStorageManager()
}

@Override
protected void doShareWith(String shareeName, ShareType shareType) {
getSupportFragmentManager().beginTransaction().replace(R.id.share_fragment_container,
FileDetailsSharingProcessFragment.newInstance(getFile(),
shareeName,
shareType),
FileDetailsSharingProcessFragment.TAG)
.commit();
override fun doShareWith(shareeName: String, shareType: ShareType) {
supportFragmentManager.beginTransaction().replace(
R.id.share_fragment_container,
newInstance(
file,
shareeName,
shareType
),
FileDetailsSharingProcessFragment.TAG
).commit()
}

/**
Expand All @@ -156,44 +149,38 @@ public class ShareActivity extends FileActivity {
* @param operation Removal operation performed.
* @param result Result of the removal.
*/
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
super.onRemoteOperationFinish(operation, result);

if (result.isSuccess() ||
(operation instanceof GetSharesForFileOperation &&
result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
)
) {
Log_OC.d(TAG, "Refreshing view on successful operation or finished refresh");
refreshSharesFromStorageManager();
override fun onRemoteOperationFinish(operation: RemoteOperation<*>?, result: RemoteOperationResult<*>) {
super.onRemoteOperationFinish(operation, result)

if (result.isSuccess || operation is GetSharesForFileOperation &&
result.code == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
) {
Log_OC.d(TAG, "Refreshing view on successful operation or finished refresh")
refreshSharesFromStorageManager()
}
}

/**
* Updates the view, reading data from {@link com.owncloud.android.datamodel.FileDataStorageManager}.
* Updates the view, reading data from [com.owncloud.android.datamodel.FileDataStorageManager].
*/
private void refreshSharesFromStorageManager() {

FileDetailSharingFragment shareFileFragment = getShareFileFragment();
if (shareFileFragment != null
&& shareFileFragment.isAdded()) { // only if added to the view hierarchy!!
shareFileFragment.refreshCapabilitiesFromDB();
shareFileFragment.refreshSharesFromDB();
private fun refreshSharesFromStorageManager() {
shareFileFragment?.let {
if (it.isAdded) {
it.refreshCapabilitiesFromDB()
it.refreshSharesFromDB()
}
}
}

/**
* Shortcut to get access to the {@link FileDetailSharingFragment} instance, if any
*
* @return A {@link FileDetailSharingFragment} instance, or null
*/
private FileDetailSharingFragment getShareFileFragment() {
return (FileDetailSharingFragment) getSupportFragmentManager().findFragmentByTag(TAG_SHARE_FRAGMENT);
private val shareFileFragment: FileDetailSharingFragment?
get() = supportFragmentManager.findFragmentByTag(TAG_SHARE_FRAGMENT) as FileDetailSharingFragment?

override fun onShareProcessClosed() {
finish()
}

@Override
public void onShareProcessClosed() {
finish();
companion object {
private val TAG = ShareActivity::class.java.simpleName
const val TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT"
}
}

0 comments on commit 9e455bf

Please sign in to comment.