X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/7c2174d09e376ecc5ebd9a510a66dc7582b173f9..6a9eaaf9aa6ce01ed788d057c56face20fa88dc3:/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index 2c4ae382..c440847c 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import android.accounts.Account; import android.accounts.AccountManager; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.BroadcastReceiver; @@ -33,11 +34,15 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; +import android.content.res.Resources.NotFoundException; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; +import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.v4.app.FragmentTransaction; import android.util.Log; @@ -46,6 +51,7 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.OnNavigationListener; @@ -77,7 +83,7 @@ import eu.alefzero.webdav.WebdavClient; */ public class FileDisplayActivity extends SherlockFragmentActivity implements - FileListFragment.ContainerActivity, OnNavigationListener, OnClickListener, android.view.View.OnClickListener { + FileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnClickListener, android.view.View.OnClickListener { private ArrayAdapter mDirectories; private OCFile mCurrentDir; @@ -86,6 +92,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private DataStorageManager mStorageManager; private SyncBroadcastReceiver mSyncBroadcastReceiver; private UploadFinishReceiver mUploadFinishReceiver; + private DownloadFinishReceiver mDownloadFinishReceiver; private View mLayoutView = null; private FileListFragment mFileList; @@ -100,23 +107,26 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private static final int DIALOG_SETUP_ACCOUNT = 0; private static final int DIALOG_CREATE_DIR = 1; private static final int DIALOG_ABOUT_APP = 2; + public static final int DIALOG_SHORT_WAIT = 3; private static final int ACTION_SELECT_FILE = 1; + private static final String TAG = "FileDisplayActivity"; + + @Override public void onCreate(Bundle savedInstanceState) { Log.i(getClass().toString(), "onCreate() start"); super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setSupportProgressBarIndeterminateVisibility(false); Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext())); if(savedInstanceState != null) { mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY); mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); - mDirectories.add("/"); + mDirectories.add(OCFile.PATH_SEPARATOR); if (mDirs != null) for (String s : mDirs) mDirectories.insert(s, 0); @@ -129,12 +139,21 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements initDelayedTilAccountAvailabe(); + // PIN CODE request ; best location is to decide, let's try this first + //if (savedInstanceState == null) { + if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) { + requestPinCode(); + } + + } else { setContentView(R.layout.no_account_available); getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE); findViewById(R.id.setup_account).setOnClickListener(this); - + + setSupportProgressBarIndeterminateVisibility(false); + Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT); intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); startActivity(intent); // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished; @@ -160,6 +179,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements break; } case R.id.startSync: { + ContentResolver.cancelSync(null, "org.owncloud"); // cancel the current synchronizations of any ownCloud account Bundle bundle = new Bundle(); bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); ContentResolver.requestSync( @@ -172,7 +192,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements action = action.setType("*/*") .addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult( - Intent.createChooser(action, "Upload file from..."), + Intent.createChooser(action, getString(R.string.upload_chooser_title)), ACTION_SELECT_FILE); break; } @@ -212,20 +232,29 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == ACTION_SELECT_FILE) { if (resultCode == RESULT_OK) { - Uri selectedImageUri = data.getData(); - - String filemanagerstring = selectedImageUri.getPath(); - String selectedImagePath = getPath(selectedImageUri); - String filepath; - - if (selectedImagePath != null) - filepath = selectedImagePath; - else - filepath = filemanagerstring; - - if (filepath == null) { - Log.e("FileDisplay", "Couldnt resolve path to file"); - return; + String filepath = null; + try { + Uri selectedImageUri = data.getData(); + + String filemanagerstring = selectedImageUri.getPath(); + String selectedImagePath = getPath(selectedImageUri); + + if (selectedImagePath != null) + filepath = selectedImagePath; + else + filepath = filemanagerstring; + + } catch (Exception e) { + Log.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e); + e.printStackTrace(); + + } finally { + if (filepath == null) { + Log.e("FileDisplay", "Couldnt resolve path to file"); + Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content), Toast.LENGTH_LONG); + t.show(); + return; + } } Intent i = new Intent(this, FileUploader.class); @@ -233,12 +262,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements AccountUtils.getCurrentOwnCloudAccount(this)); String remotepath = new String(); for (int j = mDirectories.getCount() - 2; j >= 0; --j) { - remotepath += "/" + mDirectories.getItem(j); + remotepath += OCFile.PATH_SEPARATOR + mDirectories.getItem(j); } - if (!remotepath.endsWith("/")) - remotepath += "/"; + if (!remotepath.endsWith(OCFile.PATH_SEPARATOR)) + remotepath += OCFile.PATH_SEPARATOR; remotepath += new File(filepath).getName(); - remotepath = Uri.encode(remotepath, "/"); i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath); i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath); @@ -309,6 +337,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE); mUploadFinishReceiver = new UploadFinishReceiver(); registerReceiver(mUploadFinishReceiver, uploadIntentFilter); + + // Listen for download messages + IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE); + mDownloadFinishReceiver = new DownloadFinishReceiver(); + registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); // Storage manager initialization mStorageManager = new FileDataStorageManager( @@ -358,7 +391,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements for (String s : mDirs) mDirectories.add(s); } else { - mDirectories.add("/"); + mDirectories.add(OCFile.PATH_SEPARATOR); } // Actionbar setup @@ -390,6 +423,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements unregisterReceiver(mUploadFinishReceiver); mUploadFinishReceiver = null; } + if (mDownloadFinishReceiver != null) { + unregisterReceiver(mDownloadFinishReceiver); + mDownloadFinishReceiver = null; + } + getIntent().putExtra(FileDetailFragment.EXTRA_FILE, mCurrentDir); Log.i(getClass().toString(), "onPause() end"); } @@ -410,7 +448,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements break; case DIALOG_ABOUT_APP: { builder = new AlertDialog.Builder(this); - builder.setTitle("About"); + builder.setTitle(getString(R.string.about_title)); PackageInfo pkg; try { pkg = getPackageManager().getPackageInfo(getPackageName(), 0); @@ -420,7 +458,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } catch (NameNotFoundException e) { builder = null; dialog = null; - e.printStackTrace(); + Log.e(TAG, "Error while showing about dialog", e); } break; } @@ -445,28 +483,22 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements String path; if (mCurrentDir == null) { // this is just a patch; we should ensure that mCurrentDir never is null - if (!mStorageManager.fileExists("/")) { - OCFile file = new OCFile("/"); + if (!mStorageManager.fileExists(OCFile.PATH_SEPARATOR)) { + OCFile file = new OCFile(OCFile.PATH_SEPARATOR); mStorageManager.saveFile(file); } - mCurrentDir = mStorageManager.getFileByPath("/"); + mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR); } path = FileDisplayActivity.this.mCurrentDir.getRemotePath(); // Create directory - path += Uri.encode(directoryName) + "/"; - Thread thread = new Thread(new DirectoryCreator(path, a)); + path += directoryName + OCFile.PATH_SEPARATOR; + Thread thread = new Thread(new DirectoryCreator(path, a, new Handler())); thread.start(); - - // Save new directory in local database - OCFile newDir = new OCFile(path); - newDir.setMimetype("DIR"); - newDir.setParentId(mCurrentDir.getFileId()); - mStorageManager.saveFile(newDir); - - // Display the new folder right away + dialog.dismiss(); - mFileList.listDirectory(mCurrentDir); + + showDialog(DIALOG_SHORT_WAIT); } }); builder.setNegativeButton(R.string.common_cancel, @@ -478,6 +510,15 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements dialog = builder.create(); break; } + case DIALOG_SHORT_WAIT: { + ProgressDialog working_dialog = new ProgressDialog(this); + working_dialog.setMessage(getResources().getString( + R.string.wait_a_moment)); + working_dialog.setIndeterminate(true); + working_dialog.setCancelable(false); + dialog = working_dialog; + break; + } default: dialog = null; } @@ -551,11 +592,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements private String mTargetPath; private Account mAccount; private AccountManager mAm; + private Handler mHandler; - public DirectoryCreator(String targetPath, Account account) { + public DirectoryCreator(String targetPath, Account account, Handler handler) { mTargetPath = targetPath; mAccount = account; mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE); + mHandler = handler; } @Override @@ -568,7 +611,39 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements wdc.setCredentials(username, password); wdc.allowSelfsignedCertificates(); - wdc.createDirectory(mTargetPath); + boolean created = wdc.createDirectory(mTargetPath); + if (created) { + mHandler.post(new Runnable() { + @Override + public void run() { + dismissDialog(DIALOG_SHORT_WAIT); + + // Save new directory in local database + OCFile newDir = new OCFile(mTargetPath); + newDir.setMimetype("DIR"); + newDir.setParentId(mCurrentDir.getFileId()); + mStorageManager.saveFile(newDir); + + // Display the new folder right away + mFileList.listDirectory(mCurrentDir); + } + }); + + } else { + mHandler.post(new Runnable() { + @Override + public void run() { + dismissDialog(DIALOG_SHORT_WAIT); + try { + Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG); + msg.show(); + + } catch (NotFoundException e) { + Log.e(TAG, "Error while trying to show fail message " , e); + } + } + }); + } } } @@ -608,34 +683,37 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements public void onReceive(Context context, Intent intent) { boolean inProgress = intent.getBooleanExtra( FileSyncService.IN_PROGRESS, false); - String account_name = intent + String accountName = intent .getStringExtra(FileSyncService.ACCOUNT_NAME); - Log.d("FileDisplay", "sync of account " + account_name + + Log.d("FileDisplay", "sync of account " + accountName + " is in_progress: " + inProgress); - setSupportProgressBarIndeterminateVisibility(inProgress); + + if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) { - long OCDirId = intent.getLongExtra(FileSyncService.SYNC_FOLDER, -1); - if (OCDirId >= 0) { - OCFile syncDir = mStorageManager.getFileById(OCDirId); - if (syncDir != null && ( - (mCurrentDir == null && syncDir.getFileName().equals("/")) || - syncDir.equals(mCurrentDir)) - ) { - FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); - if (fileListFragment != null) { - fileListFragment.listDirectory(); + String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); + + boolean fillBlankRoot = false; + if (mCurrentDir == null) { + mCurrentDir = mStorageManager.getFileByPath("/"); + fillBlankRoot = (mCurrentDir != null); + } + + if ((synchFolderRemotePath != null && mCurrentDir != null && (mCurrentDir.getRemotePath().equals(synchFolderRemotePath))) + || fillBlankRoot ) { + if (!fillBlankRoot) + mCurrentDir = getStorageManager().getFileByPath(synchFolderRemotePath); + FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager() + .findFragmentById(R.id.fileList); + if (fileListFragment != null) { + fileListFragment.listDirectory(mCurrentDir); } } - } - - if (!inProgress) { - FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager() - .findFragmentById(R.id.fileList); - if (fileListFragment != null) - fileListFragment.listDirectory(); + + setSupportProgressBarIndeterminateVisibility(inProgress); + } } - } @@ -649,10 +727,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements public void onReceive(Context context, Intent intent) { long parentDirId = intent.getLongExtra(FileUploader.EXTRA_PARENT_DIR_ID, -1); OCFile parentDir = mStorageManager.getFileById(parentDirId); - - if (parentDir != null && ( - (mCurrentDir == null && parentDir.getFileName().equals("/")) || - parentDir.equals(mCurrentDir)) + String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME); + + if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) && + parentDir != null && + ( (mCurrentDir == null && parentDir.getFileName().equals("/")) || + parentDir.equals(mCurrentDir) + ) ) { FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); if (fileListFragment != null) { @@ -662,6 +743,26 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } } + + + /** + * Once the file download has finished -> update view + */ + private class DownloadFinishReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH); + String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME); + + if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) && + mCurrentDir != null && mCurrentDir.getFileId() == mStorageManager.getFileByPath(downloadedRemotePath).getParentId()) { + FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); + if (fileListFragment != null) { + fileListFragment.listDirectory(); + } + } + } + } @Override @@ -717,17 +818,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements // If we are on a large device -> update fragment if (mDualPane) { - FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fileDetails == null) { - // first selected file since the current directory was listed - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); - transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); - transaction.commit(); - } else { - // another select file - fileDetails.updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this)); - } + // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous' + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); + transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); + transaction.commit(); } else { // small or medium screen device -> new Activity Intent showDetailsIntent = new Intent(this, FileDetailActivity.class); @@ -737,6 +832,19 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } } + + /** + * {@inheritDoc} + */ + @Override + public void onFileStateChanged() { + FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); + if (fileListFragment != null) { + fileListFragment.listDirectory(); + } + } + + /** * Operations in this method should be preferably performed in onCreate to have a lighter onResume method. * @@ -756,7 +864,22 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment transaction.commit(); } + setSupportProgressBarIndeterminateVisibility(false); + } + + /** + * Launch an intent to request the PIN code to the user before letting him use the app + */ + private void requestPinCode() { + boolean pinStart = false; + SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + pinStart = appPrefs.getBoolean("set_pincode", false); + if (pinStart) { + Intent i = new Intent(getApplicationContext(), PinCodeActivity.class); + i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "FileDisplayActivity"); + startActivity(i); + } }