12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313 |
- package com.owncloud.android.ui.activity;
- import java.io.File;
- import android.accounts.Account;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.app.AlertDialog.Builder;
- import android.app.Dialog;
- import android.content.BroadcastReceiver;
- import android.content.ComponentName;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.ServiceConnection;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.content.res.Resources.NotFoundException;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.preference.PreferenceManager;
- import android.provider.MediaStore;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentTransaction;
- import android.util.Log;
- import android.view.View;
- 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;
- import com.actionbarsherlock.app.SherlockFragmentActivity;
- import com.actionbarsherlock.view.Menu;
- import com.actionbarsherlock.view.MenuInflater;
- import com.actionbarsherlock.view.MenuItem;
- import com.actionbarsherlock.view.Window;
- import com.owncloud.android.AccountUtils;
- import com.owncloud.android.authenticator.AccountAuthenticator;
- import com.owncloud.android.datamodel.DataStorageManager;
- import com.owncloud.android.datamodel.FileDataStorageManager;
- import com.owncloud.android.datamodel.OCFile;
- import com.owncloud.android.files.services.FileDownloader;
- import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
- import com.owncloud.android.files.services.FileObserverService;
- import com.owncloud.android.files.services.FileUploader;
- import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
- import com.owncloud.android.network.OwnCloudClientUtils;
- import com.owncloud.android.operations.OnRemoteOperationListener;
- import com.owncloud.android.operations.RemoteOperation;
- import com.owncloud.android.operations.RemoteOperationResult;
- import com.owncloud.android.operations.RemoveFileOperation;
- import com.owncloud.android.operations.RenameFileOperation;
- import com.owncloud.android.operations.SynchronizeFileOperation;
- import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
- import com.owncloud.android.syncadapter.FileSyncService;
- import com.owncloud.android.ui.dialog.ChangelogDialog;
- import com.owncloud.android.ui.dialog.SslValidatorDialog;
- import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
- import com.owncloud.android.ui.fragment.FileDetailFragment;
- import com.owncloud.android.ui.fragment.FileFragment;
- import com.owncloud.android.ui.fragment.FilePreviewFragment;
- import com.owncloud.android.ui.fragment.OCFileListFragment;
- import com.owncloud.android.R;
- import eu.alefzero.webdav.WebdavClient;
- public class FileDisplayActivity extends SherlockFragmentActivity implements
- OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {
-
- private ArrayAdapter<String> mDirectories;
- private OCFile mCurrentDir = null;
- private OCFile mCurrentFile = null;
- private DataStorageManager mStorageManager;
- private SyncBroadcastReceiver mSyncBroadcastReceiver;
- private UploadFinishReceiver mUploadFinishReceiver;
- private DownloadFinishReceiver mDownloadFinishReceiver;
- private FileDownloaderBinder mDownloaderBinder = null;
- private FileUploaderBinder mUploaderBinder = null;
- private ServiceConnection mDownloadConnection = null, mUploadConnection = null;
- private RemoteOperationResult mLastSslUntrustedServerResult = null;
-
- private OCFileListFragment mFileList;
-
- private boolean mDualPane;
-
- 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 DIALOG_CHOOSE_UPLOAD_SOURCE = 4;
- private static final int DIALOG_SSL_VALIDATOR = 5;
- private static final int DIALOG_CERT_NOT_SAVED = 6;
- private static final String DIALOG_CHANGELOG_TAG = "DIALOG_CHANGELOG";
-
- private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
- private static final int ACTION_SELECT_MULTIPLE_FILES = 2;
-
- private static final String TAG = "FileDisplayActivity";
- private static int[] mMenuIdentifiersToPatch = {R.id.action_about_app};
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- Log.d(getClass().toString(), "onCreate() start");
- super.onCreate(savedInstanceState);
-
- mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
- Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
- if (account != null)
- AccountUtils.setCurrentOwnCloudAccount(this, account.name);
-
-
- if(savedInstanceState != null) {
-
- mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
- }
-
- if (!AccountUtils.accountsAreSetup(this)) {
-
- mStorageManager = null;
- createFirstAccount();
-
- } else {
-
- initDataFromCurrentAccount();
-
- }
-
- mUploadConnection = new ListServiceConnection();
- mDownloadConnection = new ListServiceConnection();
- bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
- bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
-
- if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) {
- requestPinCode();
- }
-
- Intent observer_intent = new Intent(this, FileObserverService.class);
- observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);
- startService(observer_intent);
-
-
-
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
-
-
- mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
- OCFile currFile = mCurrentDir;
- while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {
- mDirectories.add(currFile.getFileName());
- currFile = mStorageManager.getFileById(currFile.getParentId());
- }
- mDirectories.add(OCFile.PATH_SEPARATOR);
-
- setContentView(R.layout.files);
- mFileList = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
- mDualPane = (findViewById(R.id.file_details_container) != null);
- if (mDualPane) {
- initFileDetailsInDualPane();
- }
-
-
- ActionBar actionBar = getSupportActionBar();
- actionBar.setHomeButtonEnabled(true);
- actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getParentId() != 0);
- actionBar.setDisplayShowTitleEnabled(false);
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
- actionBar.setListNavigationCallbacks(mDirectories, this);
- setSupportProgressBarIndeterminateVisibility(false);
-
-
-
- showChangeLog();
-
- Log.d(getClass().toString(), "onCreate() end");
- }
-
-
- private void showChangeLog() {
- if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.JELLY_BEAN) {
- final String KEY_VERSION = "version";
- SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- int currentVersionNumber = 0;
- int savedVersionNumber = sharedPref.getInt(KEY_VERSION, 0);
- try {
- PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
- currentVersionNumber = pi.versionCode;
- } catch (Exception e) {}
-
- if (currentVersionNumber > savedVersionNumber) {
- ChangelogDialog.newInstance(true).show(getSupportFragmentManager(), DIALOG_CHANGELOG_TAG);
- Editor editor = sharedPref.edit();
- editor.putInt(KEY_VERSION, currentVersionNumber);
- editor.commit();
- }
- }
- }
-
-
- private void createFirstAccount() {
- 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);
- }
-
- private void initDataFromCurrentAccount() {
-
- mStorageManager = new FileDataStorageManager(
- AccountUtils.getCurrentOwnCloudAccount(this),
- getContentResolver());
-
- if(mCurrentDir != null && !mCurrentDir.isDirectory()) {
- mCurrentFile = mCurrentDir;
- mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId());
- }
-
-
- if (mCurrentDir != null) {
- mCurrentDir = mStorageManager.getFileByPath(mCurrentDir.getRemotePath());
- }
- if (mCurrentFile != null) {
- if (mCurrentFile.fileExists()) {
- mCurrentFile = mStorageManager.getFileByPath(mCurrentFile.getRemotePath());
- }
- }
-
-
- if (mCurrentDir == null) {
- mCurrentDir = mStorageManager.getFileByPath("/");
- }
- }
-
-
- private void initFileDetailsInDualPane() {
- if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) {
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- if (mCurrentFile != null) {
- if (mCurrentFile.isDown() && FilePreviewFragment.canBePreviewed(mCurrentFile)) {
- transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
- } else {
- transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
- }
- mCurrentFile = null;
-
- } else {
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG);
- }
- transaction.commit();
- }
- }
-
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (mDownloadConnection != null)
- unbindService(mDownloadConnection);
- if (mUploadConnection != null)
- unbindService(mUploadConnection);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getSherlock().getMenuInflater();
- inflater.inflate(R.menu.main_menu, menu);
-
- patchHiddenAccents(menu);
-
- return true;
- }
-
- private void patchHiddenAccents(Menu menu) {
- for (int i = 0; i < mMenuIdentifiersToPatch.length ; i++) {
- MenuItem aboutItem = menu.findItem(mMenuIdentifiersToPatch[i]);
- if (aboutItem != null && aboutItem.getIcon() instanceof BitmapDrawable) {
-
- Bitmap original = ((BitmapDrawable) aboutItem.getIcon()).getBitmap();
- float scale = getResources().getDisplayMetrics().density;
- int clippedHeight = (int) (original.getHeight() - (3 * scale));
- Bitmap scaled = Bitmap.createBitmap(original, 0, 0, original.getWidth(), clippedHeight);
- aboutItem.setIcon(new BitmapDrawable(getResources(), scaled));
- }
- }
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- boolean retval = true;
- switch (item.getItemId()) {
- case R.id.action_create_dir: {
- showDialog(DIALOG_CREATE_DIR);
- break;
- }
- case R.id.action_sync_account: {
- startSynchronization();
- break;
- }
- case R.id.action_upload: {
- showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE);
- break;
- }
- case R.id.action_settings: {
- Intent settingsIntent = new Intent(this, Preferences.class);
- startActivity(settingsIntent);
- break;
- }
- case R.id.action_about_app: {
- showDialog(DIALOG_ABOUT_APP);
- break;
- }
- case android.R.id.home: {
- if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
- onBackPressed();
- }
- break;
- }
- default:
- retval = super.onOptionsItemSelected(item);
- }
- return retval;
- }
- private void startSynchronization() {
- ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE);
- Bundle bundle = new Bundle();
- bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
- ContentResolver.requestSync(
- AccountUtils.getCurrentOwnCloudAccount(this),
- AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);
- }
- @Override
- public boolean onNavigationItemSelected(int itemPosition, long itemId) {
- int i = itemPosition;
- while (i-- != 0) {
- onBackPressed();
- }
-
-
-
- if (itemPosition != 0)
- getSupportActionBar().setSelectedNavigationItem(0);
- return true;
- }
-
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
-
- if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
- requestSimpleUpload(data, resultCode);
-
- } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
- requestMultipleUpload(data, resultCode);
-
- }
- }
- private void requestMultipleUpload(Intent data, int resultCode) {
- String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
- if (filePaths != null) {
- String[] remotePaths = new String[filePaths.length];
- String remotePathBase = "";
- for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
- remotePathBase += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
- }
- if (!remotePathBase.endsWith(OCFile.PATH_SEPARATOR))
- remotePathBase += OCFile.PATH_SEPARATOR;
- for (int j = 0; j< remotePaths.length; j++) {
- remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
- }
- Intent i = new Intent(this, FileUploader.class);
- i.putExtra(FileUploader.KEY_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
- i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
- i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
- i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
- if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
- i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
- startService(i);
-
- } else {
- Log.d("FileDisplay", "User clicked on 'Update' with no selection");
- Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected), Toast.LENGTH_LONG);
- t.show();
- return;
- }
- }
- private void requestSimpleUpload(Intent data, int resultCode) {
- 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);
- i.putExtra(FileUploader.KEY_ACCOUNT,
- AccountUtils.getCurrentOwnCloudAccount(this));
- String remotepath = new String();
- for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
- remotepath += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
- }
- if (!remotepath.endsWith(OCFile.PATH_SEPARATOR))
- remotepath += OCFile.PATH_SEPARATOR;
- remotepath += new File(filepath).getName();
- i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
- i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
- i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
- if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
- i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
- startService(i);
- }
- @Override
- public void onBackPressed() {
- if (mDirectories.getCount() <= 1) {
- finish();
- return;
- }
- popDirname();
- mFileList.onNavigateUp();
- mCurrentDir = mFileList.getCurrentFile();
-
- if (mDualPane) {
-
- Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (fileFragment != null && (fileFragment instanceof FilePreviewFragment || !((FileDetailFragment) fileFragment).isEmpty())) {
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG);
- transaction.commit();
- }
- }
-
- if(mCurrentDir.getParentId() == 0){
- ActionBar actionBar = getSupportActionBar();
- actionBar.setDisplayHomeAsUpEnabled(false);
- }
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
-
- Log.d(getClass().toString(), "onSaveInstanceState() start");
- super.onSaveInstanceState(outState);
- outState.putParcelable(FileDetailFragment.EXTRA_FILE, mCurrentDir);
- if (mDualPane) {
- FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (fragment != null) {
- OCFile file = fragment.getFile();
- if (file != null) {
- outState.putParcelable(FileDetailFragment.EXTRA_FILE, file);
- }
- }
- }
- Log.d(getClass().toString(), "onSaveInstanceState() end");
- }
- @Override
- protected void onResume() {
- Log.d(getClass().toString(), "onResume() start");
- super.onResume();
- if (AccountUtils.accountsAreSetup(this)) {
-
- if (mStorageManager == null) {
-
- initDataFromCurrentAccount();
- if (mDualPane) {
- initFileDetailsInDualPane();
- }
- }
-
-
- IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
- mSyncBroadcastReceiver = new SyncBroadcastReceiver();
- registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
-
-
- IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
- mUploadFinishReceiver = new UploadFinishReceiver();
- registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
-
-
- IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
- mDownloadFinishReceiver = new DownloadFinishReceiver();
- registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
-
-
- mFileList.listDirectory(mCurrentDir);
-
- } else {
-
- mStorageManager = null;
- showDialog(DIALOG_SETUP_ACCOUNT);
-
- }
- Log.d(getClass().toString(), "onResume() end");
- }
-
- @Override
- protected void onPause() {
- Log.d(getClass().toString(), "onPause() start");
- super.onPause();
- if (mSyncBroadcastReceiver != null) {
- unregisterReceiver(mSyncBroadcastReceiver);
- mSyncBroadcastReceiver = null;
- }
- if (mUploadFinishReceiver != null) {
- unregisterReceiver(mUploadFinishReceiver);
- mUploadFinishReceiver = null;
- }
- if (mDownloadFinishReceiver != null) {
- unregisterReceiver(mDownloadFinishReceiver);
- mDownloadFinishReceiver = null;
- }
- if (!AccountUtils.accountsAreSetup(this)) {
- dismissDialog(DIALOG_SETUP_ACCOUNT);
- }
-
- Log.d(getClass().toString(), "onPause() end");
- }
-
- @Override
- protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
- if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {
- ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);
- }
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
- Dialog dialog = null;
- AlertDialog.Builder builder;
- switch (id) {
- case DIALOG_SETUP_ACCOUNT: {
- builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.main_tit_accsetup);
- builder.setMessage(R.string.main_wrn_accsetup);
- builder.setCancelable(false);
- builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- createFirstAccount();
- dialog.dismiss();
- }
- });
- String message = String.format(getString(R.string.common_exit), getString(R.string.app_name));
- builder.setNegativeButton(message, new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- finish();
- }
- });
-
- dialog = builder.create();
- break;
- }
- case DIALOG_ABOUT_APP: {
- builder = new AlertDialog.Builder(this);
- builder.setTitle(getString(R.string.about_title));
- PackageInfo pkg;
- try {
- pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
- builder.setMessage(String.format(getString(R.string.about_message), getString(R.string.app_name), pkg.versionName));
- builder.setIcon(android.R.drawable.ic_menu_info_details);
- dialog = builder.create();
- } catch (NameNotFoundException e) {
- builder = null;
- dialog = null;
- Log.e(TAG, "Error while showing about dialog", e);
- }
- break;
- }
- case DIALOG_CREATE_DIR: {
- builder = new Builder(this);
- final EditText dirNameInput = new EditText(getBaseContext());
- builder.setView(dirNameInput);
- builder.setTitle(R.string.uploader_info_dirname);
- int typed_color = getResources().getColor(R.color.setup_text_typed);
- dirNameInput.setTextColor(typed_color);
- builder.setPositiveButton(android.R.string.ok,
- new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- String directoryName = dirNameInput.getText().toString();
- if (directoryName.trim().length() == 0) {
- dialog.cancel();
- return;
- }
-
-
- String path;
- if (mCurrentDir == null) {
-
- if (!mStorageManager.fileExists(OCFile.PATH_SEPARATOR)) {
- OCFile file = new OCFile(OCFile.PATH_SEPARATOR);
- mStorageManager.saveFile(file);
- }
- mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
- }
- path = FileDisplayActivity.this.mCurrentDir.getRemotePath();
-
-
- path += directoryName + OCFile.PATH_SEPARATOR;
- Thread thread = new Thread(new DirectoryCreator(path, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this), new Handler()));
- thread.start();
-
- dialog.dismiss();
-
- showDialog(DIALOG_SHORT_WAIT);
- }
- });
- builder.setNegativeButton(R.string.common_cancel,
- new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- 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;
- }
- case DIALOG_CHOOSE_UPLOAD_SOURCE: {
- final String [] items = { getString(R.string.actionbar_upload_files),
- getString(R.string.actionbar_upload_from_apps) };
- builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.actionbar_upload);
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- if (item == 0) {
-
- Intent action = new Intent(FileDisplayActivity.this, UploadFilesActivity.class);
- action.putExtra(UploadFilesActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this));
- startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);
-
-
-
- } else if (item == 1) {
- Intent action = new Intent(Intent.ACTION_GET_CONTENT);
- action = action.setType("*/*")
- .addCategory(Intent.CATEGORY_OPENABLE);
- startActivityForResult(
- Intent.createChooser(action, getString(R.string.upload_chooser_title)),
- ACTION_SELECT_CONTENT_FROM_APPS);
- }
- }
- });
- dialog = builder.create();
- break;
- }
- case DIALOG_SSL_VALIDATOR: {
- dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);
- break;
- }
- case DIALOG_CERT_NOT_SAVED: {
- builder = new AlertDialog.Builder(this);
- builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
- builder.setCancelable(false);
- builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- };
- });
- dialog = builder.create();
- break;
- }
- default:
- dialog = null;
- }
-
- return dialog;
- }
-
-
- public String getPath(Uri uri) {
- String[] projection = { MediaStore.Images.Media.DATA };
- Cursor cursor = managedQuery(uri, projection, null, null, null);
- if (cursor != null) {
- int column_index = cursor
- .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- return cursor.getString(column_index);
- }
- return null;
- }
-
-
- public void pushDirname(OCFile directory) {
- if(!directory.isDirectory()){
- throw new IllegalArgumentException("Only directories may be pushed!");
- }
- mDirectories.insert(directory.getFileName(), 0);
- mCurrentDir = directory;
- }
-
- public boolean popDirname() {
- mDirectories.remove(mDirectories.getItem(0));
- return !mDirectories.isEmpty();
- }
- private class DirectoryCreator implements Runnable {
- private String mTargetPath;
- private Account mAccount;
- private Handler mHandler;
-
- public DirectoryCreator(String targetPath, Account account, Handler handler) {
- mTargetPath = targetPath;
- mAccount = account;
- mHandler = handler;
- }
-
- @Override
- public void run() {
- WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
- boolean created = wdc.createDirectory(mTargetPath);
- if (created) {
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- dismissDialog(DIALOG_SHORT_WAIT);
-
-
- OCFile newDir = new OCFile(mTargetPath);
- newDir.setMimetype("DIR");
- newDir.setParentId(mCurrentDir.getFileId());
- mStorageManager.saveFile(newDir);
-
-
- mFileList.listDirectory();
- }
- });
-
- } 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);
- }
- }
- });
- }
- }
-
- }
-
- private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
-
- public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
- super(ctx, view);
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- View v = super.getView(position, convertView, parent);
-
- ((TextView) v).setTextColor(getResources().getColorStateList(
- android.R.color.white));
- return v;
- }
-
- public View getDropDownView(int position, View convertView,
- ViewGroup parent) {
- View v = super.getDropDownView(position, convertView, parent);
-
- ((TextView) v).setTextColor(getResources().getColorStateList(
- android.R.color.white));
-
- return v;
- }
-
- }
- private class SyncBroadcastReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- boolean inProgress = intent.getBooleanExtra(
- FileSyncService.IN_PROGRESS, false);
- String accountName = intent
- .getStringExtra(FileSyncService.ACCOUNT_NAME);
- Log.d("FileDisplay", "sync of account " + accountName
- + " is in_progress: " + inProgress);
- if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {
-
- 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);
- OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager()
- .findFragmentById(R.id.fileList);
- if (fileListFragment != null) {
- fileListFragment.listDirectory(mCurrentDir);
- }
- }
-
- setSupportProgressBarIndeterminateVisibility(inProgress);
- removeStickyBroadcast(intent);
-
- }
-
- RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncService.SYNC_RESULT);
- if (synchResult != null) {
- if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
- mLastSslUntrustedServerResult = synchResult;
- showDialog(DIALOG_SSL_VALIDATOR);
- }
- }
- }
- }
-
- private class UploadFinishReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
- String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
- boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);
- boolean isDescendant = (mCurrentDir != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(mCurrentDir.getRemotePath()));
- if (sameAccount && isDescendant) {
- OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
- if (fileListFragment != null) {
- fileListFragment.listDirectory();
- }
- }
- }
-
- }
-
-
-
- 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);
- boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);
- boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));
- if (sameAccount && isDescendant) {
- OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
- if (fileListFragment != null) {
- fileListFragment.listDirectory();
- }
- }
- }
- }
-
-
-
-
-
- @Override
- public DataStorageManager getStorageManager() {
- return mStorageManager;
- }
-
-
-
- @Override
- public void onDirectoryClick(OCFile directory) {
- pushDirname(directory);
- ActionBar actionBar = getSupportActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
-
- if (mDualPane) {
-
- Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (fileFragment != null && (fileFragment instanceof FilePreviewFragment || !((FileDetailFragment) fileFragment).isEmpty())) {
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG);
- transaction.commit();
- }
- }
- }
-
-
-
- @Override
- public void onFileClick(OCFile file) {
-
- if (mDualPane) {
-
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- if (file != null && file.isDown() && FilePreviewFragment.canBePreviewed(file)) {
- transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
- } else {
- transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
- }
-
- transaction.commit();
-
- } else {
- Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
- showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
- showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
- startActivity(showDetailsIntent);
- }
- }
-
-
-
- @Override
- public OCFile getInitialDirectory() {
- return mCurrentDir;
- }
-
-
-
- @Override
- public void onFileStateChanged() {
- OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
- if (fileListFragment != null) {
- fileListFragment.listDirectory();
- }
- }
-
-
- @Override
- public FileDownloaderBinder getFileDownloaderBinder() {
- return mDownloaderBinder;
- }
-
-
- @Override
- public FileUploaderBinder getFileUploaderBinder() {
- return mUploaderBinder;
- }
-
-
-
- private class ListServiceConnection implements ServiceConnection {
- @Override
- public void onServiceConnected(ComponentName component, IBinder service) {
- if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
- Log.d(TAG, "Download service connected");
- mDownloaderBinder = (FileDownloaderBinder) service;
- } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
- Log.d(TAG, "Upload service connected");
- mUploaderBinder = (FileUploaderBinder) service;
- } else {
- return;
- }
-
- if (mFileList != null)
- mFileList.listDirectory();
- if (mDualPane) {
- Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (fragment != null && fragment instanceof FileDetailFragment) {
- ((FileDetailFragment)fragment).updateFileDetails(false);
- }
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName component) {
- if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
- Log.d(TAG, "Download service disconnected");
- mDownloaderBinder = null;
- } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
- Log.d(TAG, "Upload service disconnected");
- mUploaderBinder = null;
- }
- }
- };
-
-
-
- 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);
- }
- }
- @Override
- public void onSavedCertificate() {
- startSynchronization();
- }
- @Override
- public void onFailedSavingCertificate() {
- showDialog(DIALOG_CERT_NOT_SAVED);
- }
-
- @Override
- public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
- if (operation instanceof RemoveFileOperation) {
- onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
-
- } else if (operation instanceof RenameFileOperation) {
- onRenameFileOperationFinish((RenameFileOperation)operation, result);
-
- } else if (operation instanceof SynchronizeFileOperation) {
- onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);
- }
- }
-
- private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
- dismissDialog(DIALOG_SHORT_WAIT);
- if (result.isSuccess()) {
- Toast msg = Toast.makeText(this, R.string.remove_success_msg, Toast.LENGTH_LONG);
- msg.show();
- OCFile removedFile = operation.getFile();
- if (mDualPane) {
- FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (details != null && removedFile.equals(details.getFile())) {
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null));
- transaction.commit();
- }
- }
- if (mStorageManager.getFileById(removedFile.getParentId()).equals(mCurrentDir)) {
- mFileList.listDirectory();
- }
-
- } else {
- Toast msg = Toast.makeText(this, R.string.remove_fail_msg, Toast.LENGTH_LONG);
- msg.show();
- if (result.isSslRecoverableException()) {
- mLastSslUntrustedServerResult = result;
- showDialog(DIALOG_SSL_VALIDATOR);
- }
- }
- }
-
- private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {
- dismissDialog(DIALOG_SHORT_WAIT);
- OCFile renamedFile = operation.getFile();
- if (result.isSuccess()) {
- if (mDualPane) {
- FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (details != null && details instanceof FileDetailFragment && renamedFile.equals(details.getFile()) ) {
- ((FileDetailFragment) details).updateFileDetails(renamedFile, AccountUtils.getCurrentOwnCloudAccount(this));
- }
- }
- if (mStorageManager.getFileById(renamedFile.getParentId()).equals(mCurrentDir)) {
- mFileList.listDirectory();
- }
-
- } else {
- if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
- Toast msg = Toast.makeText(this, R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
- msg.show();
-
- } else {
- Toast msg = Toast.makeText(this, R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
- msg.show();
- if (result.isSslRecoverableException()) {
- mLastSslUntrustedServerResult = result;
- showDialog(DIALOG_SSL_VALIDATOR);
- }
- }
- }
- }
- private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {
- dismissDialog(DIALOG_SHORT_WAIT);
- OCFile syncedFile = operation.getLocalFile();
- if (!result.isSuccess()) {
- if (result.getCode() == ResultCode.SYNC_CONFLICT) {
- Intent i = new Intent(this, ConflictsResolveActivity.class);
- i.putExtra(ConflictsResolveActivity.EXTRA_FILE, syncedFile);
- i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
- startActivity(i);
-
- } else {
- Toast msg = Toast.makeText(this, R.string.sync_file_fail_msg, Toast.LENGTH_LONG);
- msg.show();
- }
-
- } else {
- if (operation.transferWasRequested()) {
- mFileList.listDirectory();
- onTransferStateChanged(syncedFile, true, true);
-
- } else {
- Toast msg = Toast.makeText(this, R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG);
- msg.show();
- }
- }
- }
-
- @Override
- public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
-
- if (mDualPane) {
- FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
- if (details != null && details instanceof FileDetailFragment && file.equals(details.getFile()) ) {
- if (downloading || uploading) {
- ((FileDetailFragment)details).updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));
- } else {
- ((FileDetailFragment)details).updateFileDetails(downloading || uploading);
- }
- }
- }
- }
- @Override
- public void showFragmentWithDetails(OCFile file) {
- if (mDualPane) {
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
- transaction.commit();
-
- } else {
- Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
- showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
- showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
- showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
- startActivity(showDetailsIntent);
- }
- }
- }
|