PreviewImageActivity protected against access to non-existing account
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 14 Jun 2013 10:53:45 +0000 (12:53 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 14 Jun 2013 10:53:45 +0000 (12:53 +0200)
src/com/owncloud/android/ui/activity/FileActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/preview/PreviewImageActivity.java

index 8adb9ba..52f5c2b 100644 (file)
@@ -60,6 +60,12 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
     private boolean mRedirectingToSetupAccount = false;
     
+    /** Flag to signal when the value of mAccount was set */ 
+    private boolean mAccountWasSet;
+    
+    /** Flag to signal when the value of mAccount was restored from a saved state */ 
+    private boolean mAccountWasRestored;
+    
 
     /**
      * Loads the cownCloud {@link Account} and main {@link OCFile} to be handled by the instance of 
@@ -80,9 +86,14 @@ public abstract class FileActivity extends SherlockFragmentActivity {
             mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
         }
 
+        Account oldAccount = mAccount;
         grantValidAccount();
         if (mAccount != null) {
-            onAccountSet(savedInstanceState != null);
+            mAccountWasSet = true;
+            mAccountWasRestored = (savedInstanceState != null && mAccount.equals(oldAccount));
+        } else {
+            mAccountWasSet = false;
+            mAccountWasRestored = false;
         }
     }
 
@@ -99,11 +110,15 @@ public abstract class FileActivity extends SherlockFragmentActivity {
         Account oldAccount = mAccount;
         grantValidAccount();
         if (mAccount != null && !mAccount.equals(oldAccount)) {
-            onAccountSet(false);
+            mAccountWasSet = true;
+            mAccountWasRestored = false;
+        } else {
+            mAccountWasSet = false;
+            mAccountWasRestored = false;
         }
     }
+
     
-        
     /**
      *  Validates the ownCloud {@link Account} associated to the Activity any time it is restarted.
      * 
@@ -126,6 +141,16 @@ public abstract class FileActivity extends SherlockFragmentActivity {
     }
     
     
+    @Override 
+    protected void onStart() {
+        // maybe better in onPostCreate() ?
+        super.onStart();
+        if (mAccountWasSet) {
+            onAccountSet(mAccountWasRestored);
+        }
+    }
+    
+    
     /**
      * Launches the account creation activity. To use when no ownCloud account is available
      */
@@ -214,7 +239,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
                         FileActivity.this.onAccountSet(false);
                     }
                 } catch (OperationCanceledException e) {
-                    Log_OC.e(TAG, "Account creation canceled");
+                    Log_OC.d(TAG, "Account creation canceled");
                     
                 } catch (Exception e) {
                     Log_OC.e(TAG, "Account creation finished in exception: ", e);
@@ -224,13 +249,7 @@ public abstract class FileActivity extends SherlockFragmentActivity {
                 Log_OC.e(TAG, "Account creation callback with null bundle");
             }
             if (mAccount == null) {
-                if (isTaskRoot()) {
-                    Log_OC.e(TAG, "FINISHING");
-                    finish();
-                } else {
-                    Log_OC.e(TAG, "MOVING BACK");
-                    moveTaskToBack(true);
-                }
+                moveTaskToBack(true);
             }
         }
         
index 9896a92..9fc631a 100644 (file)
@@ -137,9 +137,9 @@ public class FileDisplayActivity extends FileActivity implements
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         Log_OC.d(TAG, "onCreate() start");
-        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+        super.onCreate(savedInstanceState);
         
-        super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid
+        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
         
         mHandler = new Handler();
 
@@ -176,26 +176,12 @@ public class FileDisplayActivity extends FileActivity implements
         mRightFragmentContainer = findViewById(R.id.right_fragment_container);
         if (savedInstanceState == null) {
             createMinFragments();
-            if (!isRedirectingToSetupAccount()) {
-                initFragmentsWithFile();
-            }
         }
         
         // Action bar setup
         mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
-        OCFile currFile = getFile();
-        if (mStorageManager != null) {
-            while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {
-                if (currFile.isDirectory()) {
-                    mDirectories.add(currFile.getFileName());
-                }
-                currFile = mStorageManager.getFileById(currFile.getParentId());
-            }
-        }
-        mDirectories.add(OCFile.PATH_SEPARATOR);
-        ActionBar actionBar = getSupportActionBar();
-        actionBar.setHomeButtonEnabled(true);   // mandatory since Android ICS, according to the official documentation
-        setSupportProgressBarIndeterminateVisibility(false);        // always AFTER setContentView(...) ; to workaround bug in its implementation
+        getSupportActionBar().setHomeButtonEnabled(true);       // mandatory since Android ICS, according to the official documentation
+        setSupportProgressBarIndeterminateVisibility(false);    // always AFTER setContentView(...) ; to work around bug in its implementation
         
         Log_OC.d(TAG, "onCreate() end");
     }
@@ -226,15 +212,20 @@ public class FileDisplayActivity extends FileActivity implements
             }
             if (file == null) {
                 // fall back to root folder
-                file = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);  // never should return null
+                file = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);  // never returns null
             }
             setFile(file);
             
-            if (findViewById(android.R.id.content) != null && !stateWasRecovered) {
+            while(file != null && file.getFileName() != OCFile.PATH_SEPARATOR) {
+                if (file.isDirectory()) {
+                    mDirectories.add(file.getFileName());
+                }
+                file = mStorageManager.getFileById(file.getParentId());
+            }
+            mDirectories.add(OCFile.PATH_SEPARATOR);
+            if (!stateWasRecovered) {
                 Log_OC.e(TAG, "Initializing Fragments in onAccountChanged..");
                 initFragmentsWithFile();
-            } else {
-                Log_OC.e(TAG, "Fragment initializacion ignored in onAccountChanged due to lack of CONTENT VIEW");
             }
             
         } else {
@@ -589,7 +580,7 @@ public class FileDisplayActivity extends FileActivity implements
         outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
         Log_OC.d(TAG, "onSaveInstanceState() end");
     }
-    
+
     @Override
     protected void onStart() {
         super.onStart();
index a41f6eb..b9ede07 100644 (file)
@@ -16,7 +16,6 @@
  */
 package com.owncloud.android.ui.preview;
 
-import android.accounts.Account;
 import android.app.Dialog;
 import android.app.ProgressDialog;
 import android.content.BroadcastReceiver;
@@ -33,7 +32,6 @@ import android.view.View;
 import android.view.View.OnTouchListener;
 
 import com.actionbarsherlock.app.ActionBar;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.actionbarsherlock.view.MenuItem;
 import com.actionbarsherlock.view.Window;
 import com.owncloud.android.datamodel.DataStorageManager;
@@ -46,13 +44,12 @@ import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.fragment.FileFragment;
-
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.Log_OC;
 import com.owncloud.android.R;
 
 /**
- *  Used as an utility to preview image files contained in an ownCloud account.
+ *  Holds a swiping galley where image files contained in an ownCloud directory are shown
  *  
  *  @author David A. Velasco
  */
@@ -65,9 +62,6 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C
     public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
     private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
     
-    private OCFile mFile;
-    private OCFile mParentFolder;  
-    private Account mAccount;
     private DataStorageManager mStorageManager;
     
     private ViewPager mViewPager; 
@@ -88,53 +82,35 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
-        mAccount = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
-        if (mFile == null) {
-            throw new IllegalStateException("Instanced with a NULL OCFile");
-        }
-        if (mAccount == null) {
-            throw new IllegalStateException("Instanced with a NULL ownCloud Account");
-        }
-        if (!mFile.isImage()) {
-            throw new IllegalArgumentException("Non-image file passed as argument");
-        }
         requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
         setContentView(R.layout.preview_image_activity);
-    
+        
         ActionBar actionBar = getSupportActionBar();
         actionBar.setDisplayHomeAsUpEnabled(true);
-        actionBar.setTitle(mFile.getFileName());
         actionBar.hide();
         
         mFullScreen = true;
-        
-        mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
-        mParentFolder = mStorageManager.getFileById(mFile.getParentId());
-        if (mParentFolder == null) {
-            // should not be necessary
-            mParentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
-        }
-
         if (savedInstanceState != null) {
             mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
         } else {
             mRequestWaitingForBinder = false;
         }
-        
-        createViewPager();
-
     }
 
-    private void createViewPager() {
-        mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder, mAccount, mStorageManager);
+    private void initViewPager() {
+        OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
+        if (parentFolder == null) {
+            // should not be necessary
+            parentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
+        }
+        mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder, getAccount(), mStorageManager);
         mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
-        int position = mPreviewImagePagerAdapter.getFilePosition(mFile);
+        int position = mPreviewImagePagerAdapter.getFilePosition(getFile());
         position = (position >= 0) ? position : 0;
         mViewPager.setAdapter(mPreviewImagePagerAdapter); 
         mViewPager.setOnPageChangeListener(this);
         mViewPager.setCurrentItem(position);
-        if (position == 0 && !mFile.isDown()) {
+        if (position == 0 && !getFile().isDown()) {
             // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0) 
             mRequestWaitingForBinder = true;
         }
@@ -320,9 +296,9 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C
         if (mDownloaderBinder == null) {
             Log_OC.d(TAG, "requestForDownload called without binder to download service");
             
-        } else if (!mDownloaderBinder.isDownloading(mAccount, file)) {
+        } else if (!mDownloaderBinder.isDownloading(getAccount(), file)) {
             Intent i = new Intent(this, FileDownloader.class);
-            i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
+            i.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
             i.putExtra(FileDownloader.EXTRA_FILE, file);
             startService(i);
         }
@@ -385,7 +361,7 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C
         public void onReceive(Context context, Intent intent) {
             String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
             String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
-            if (mAccount.name.equals(accountName) && 
+            if (getAccount().name.equals(accountName) && 
                     downloadedRemotePath != null) {
 
                 OCFile file = mStorageManager.getFileByPath(downloadedRemotePath);
@@ -436,7 +412,33 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C
 
     @Override
     protected void onAccountSet(boolean stateWasRecovered) {
-        // TODO
+        if (getAccount() != null) {
+            OCFile file = getFile();
+            /// Validate handled file  (first image to preview)
+            if (file == null) {
+                throw new IllegalStateException("Instanced with a NULL OCFile");
+            }
+            if (!file.isImage()) {
+                throw new IllegalArgumentException("Non-image file passed as argument");
+            }
+            mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
+            file = mStorageManager.getFileById(file.getFileId()); 
+            if (file != null) {
+                /// Refresh the activity according to the Account and OCFile set
+                setFile(file);  // reset after getting it fresh from mStorageManager
+                getSupportActionBar().setTitle(getFile().getFileName());
+                //if (!stateWasRecovered) {
+                    initViewPager();
+                //}
+
+            } else {
+                // handled file not in the current Account
+                finish();
+            }
+            
+        } else {
+            Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
+        }
     }