Merge pull request #284 from owncloud/setup_app_name
authorDavid A. Velasco <dvelasco@owncloud.com>
Tue, 22 Oct 2013 12:15:21 +0000 (05:15 -0700)
committerDavid A. Velasco <dvelasco@owncloud.com>
Tue, 22 Oct 2013 12:15:21 +0000 (05:15 -0700)
Setup app name, optional settings, and availability of multiple accounts.

1  2 
AndroidManifest.xml
src/com/owncloud/android/files/services/FileUploader.java
src/com/owncloud/android/operations/SynchronizeFolderOperation.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java
src/com/owncloud/android/ui/preview/PreviewImageActivity.java

diff --combined AndroidManifest.xml
@@@ -18,8 -18,8 +18,8 @@@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   -->
  <manifest package="com.owncloud.android"
 -    android:versionCode="104005"
 -    android:versionName="1.4.5" xmlns:android="http://schemas.android.com/apk/res/android">
 +    android:versionCode="104006"
 +    android:versionName="1.4.6" xmlns:android="http://schemas.android.com/apk/res/android">
  
      <uses-permission android:name="android.permission.GET_ACCOUNTS" />
      <uses-permission android:name="android.permission.USE_CREDENTIALS" />
@@@ -44,6 -44,7 +44,7 @@@
      </uses-permission>
  
      <application
+         android:name=".MainApp"
          android:icon="@drawable/icon"
          android:label="@string/app_name"
          android:theme="@style/Theme.ownCloud"> 
  
          <provider
              android:name=".providers.FileContentProvider"
-             android:authorities="org.owncloud"
+             android:authorities="@string/authority"
              android:enabled="true"
              android:exported="false"
              android:label="@string/sync_string_files"
@@@ -29,7 -29,6 +29,7 @@@ import java.util.concurrent.ConcurrentH
  import java.util.concurrent.ConcurrentMap;
  
  import org.apache.http.HttpStatus;
 +import org.apache.jackrabbit.webdav.DavConstants;
  import org.apache.jackrabbit.webdav.MultiStatus;
  import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  
@@@ -71,6 -70,7 +71,7 @@@ import android.webkit.MimeTypeMap
  import android.widget.RemoteViews;
  
  import com.owncloud.android.Log_OC;
+ import com.owncloud.android.MainApp;
  import com.owncloud.android.R;
  import com.owncloud.android.db.DbHandler;
  import com.owncloud.android.ui.activity.FailedUploadActivity;
@@@ -84,7 -84,7 +85,7 @@@ import eu.alefzero.webdav.WebdavClient
  
  public class FileUploader extends Service implements OnDatatransferProgressListener {
  
-     public static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
+     private static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
      public static final String EXTRA_UPLOAD_RESULT = "RESULT";
      public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
      public static final String EXTRA_OLD_REMOTE_PATH = "OLD_REMOTE_PATH";
      private int mLastPercent;
      private RemoteViews mDefaultNotificationContentView;
  
+     
+     public String getUploadFinishMessage() {
+         return getClass().getName().toString() + UPLOAD_FINISH_MESSAGE;
+     }
+     
      /**
       * Builds a key for mPendingUploads from the account and file to upload
       * 
                      mPendingUploads.remove(uploadKey);
                      Log_OC.i(TAG, "Remove CurrentUploadItem from pending upload Item Map.");
                  }
 +                if (uploadResult.isException()) {
 +                    // enforce the creation of a new client object for next uploads; this grant that a new socket will 
 +                    // be created in the future if the current exception is due to an abrupt lose of network connection
 +                    mUploadClient = null;
 +                }
              }
              
              /// notify result
          PropFindMethod propfind = null;
          RemoteOperationResult result = null;
          try {
 -            propfind = new PropFindMethod(mUploadClient.getBaseUri()
 -                    + WebdavUtils.encodePath(mCurrentUpload.getRemotePath()));
 +            propfind = new PropFindMethod(mUploadClient.getBaseUri() + WebdavUtils.encodePath(mCurrentUpload.getRemotePath()),
 +                    DavConstants.PROPFIND_ALL_PROP,
 +                    DavConstants.DEPTH_0);
              int status = mUploadClient.executeMethod(propfind);
              boolean isMultiStatus = (status == HttpStatus.SC_MULTI_STATUS);
              if (isMultiStatus) {
          mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
          
          /// includes a pending intent in the notification showing the details view of the file
 -        Intent showDetailsIntent = null;
 -        if (PreviewImageFragment.canBePreviewed(upload.getFile())) {
 -            showDetailsIntent = new Intent(this, PreviewImageActivity.class);
 -        } else {
 -            showDetailsIntent = new Intent(this, FileDisplayActivity.class);
 -        }
 +        Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class);
          showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, upload.getFile());
          showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, upload.getAccount());
          showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              boolean needsToUpdateCredentials = (uploadResult.getCode() == ResultCode.UNAUTHORIZED ||
                      //(uploadResult.isTemporalRedirection() && uploadResult.isIdPRedirection() && 
                      (uploadResult.isIdPRedirection() &&
-                             AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mUploadClient.getAuthTokenType())));
+                             MainApp.getAuthTokenTypeSamlSessionCookie().equals(mUploadClient.getAuthTokenType())));
              if (needsToUpdateCredentials) {
                  // let the user update credentials with one click
                  Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
       * @param uploadResult Result of the upload operation
       */
      private void sendFinalBroadcast(UploadFileOperation upload, RemoteOperationResult uploadResult) {
-         Intent end = new Intent(UPLOAD_FINISH_MESSAGE);
+         Intent end = new Intent(getUploadFinishMessage());
          end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath()); // real remote
                                                                   // path, after
                                                                   // possible
@@@ -28,9 -28,7 +28,8 @@@ import java.util.List
  import java.util.Map;
  import java.util.Vector;
  
- import org.apache.commons.httpclient.Header;
  import org.apache.http.HttpStatus;
 +import org.apache.jackrabbit.webdav.DavConstants;
  import org.apache.jackrabbit.webdav.MultiStatus;
  import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  
@@@ -136,9 -134,7 +135,9 @@@ public class SynchronizeFolderOperatio
              Log_OC.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath);
              
              // remote request 
 -            query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
 +            query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
 +                    DavConstants.PROPFIND_ALL_PROP,
 +                    DavConstants.DEPTH_1);
              int status = client.executeMethod(query);
              
              // check and process response   - /// TODO take into account all the possible status per child-resource
@@@ -58,8 -58,8 +58,8 @@@ import com.actionbarsherlock.view.MenuI
  import com.actionbarsherlock.view.MenuItem;
  import com.actionbarsherlock.view.Window;
  import com.owncloud.android.Log_OC;
+ import com.owncloud.android.MainApp;
  import com.owncloud.android.R;
- import com.owncloud.android.authentication.AccountAuthenticator;
  import com.owncloud.android.datamodel.DataStorageManager;
  import com.owncloud.android.datamodel.FileDataStorageManager;
  import com.owncloud.android.datamodel.OCFile;
@@@ -86,7 -86,6 +86,7 @@@ import com.owncloud.android.ui.fragment
  import com.owncloud.android.ui.fragment.FileFragment;
  import com.owncloud.android.ui.fragment.OCFileListFragment;
  import com.owncloud.android.ui.preview.PreviewImageActivity;
 +import com.owncloud.android.ui.preview.PreviewImageFragment;
  import com.owncloud.android.ui.preview.PreviewMediaFragment;
  import com.owncloud.android.ui.preview.PreviewVideoActivity;
  
@@@ -138,6 -137,9 +138,9 @@@ OCFileListFragment.ContainerActivity, F
  
      private OCFile mWaitingToPreview;
      private Handler mHandler;
+     
+     private String mDownloadAddedMessage;
+     private String mDownloadFinishMessage;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid
  
          mHandler = new Handler();
+         
+         FileDownloader downloader = new FileDownloader();
+         mDownloadAddedMessage = downloader.getDownloadAddedMessage();
+         mDownloadFinishMessage= downloader.getDownloadFinishMessage();
  
          /// bindings to transference services
          mUploadConnection = new ListServiceConnection(); 
          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");
      }
  
  
              /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
              OCFile file = getFile();
 +            // get parent from path
 +            String parentPath = "";
              if (file != null) {
                  if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
                      // upload in progress - right now, files are not inserted in the local cache until the upload is successful
 -                    if (mStorageManager.getFileById(file.getParentId()) == null) {
 -                        file = null;    // not able to know the directory where the file is uploading
 -                    }
 +                    // get parent from path
 +                    parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
 +                    if (mStorageManager.getFileByPath(parentPath) ==  null)
 +                        file = null; // not able to know the directory where the file is uploading
                  } else {
                      file = mStorageManager.getFileByPath(file.getRemotePath());   // currentDir = null if not in the current Account
                  }
                  if (fileIt.isDirectory()) {
                      mDirectories.add(fileIt.getFileName());
                  }
 -                fileIt = mStorageManager.getFileById(fileIt.getParentId());
 +                // get parent from path
 +                parentPath = fileIt.getRemotePath().substring(0, fileIt.getRemotePath().lastIndexOf(fileIt.getFileName()));
 +                fileIt = mStorageManager.getFileByPath(parentPath);
              }
              mDirectories.add(OCFile.PATH_SEPARATOR);
              if (!stateWasRecovered) {
                  // the user browsed to other file ; forget the automatic preview 
                  mWaitingToPreview = null;
  
-             } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
+             } else if (downloadEvent.equals(mDownloadAddedMessage)) {
                  // grant that the right panel updates the progress bar
                  detailsFragment.listenForTransferProgress();
                  detailsFragment.updateFileDetails(true, false);
  
-             } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
+             } else if (downloadEvent.equals(mDownloadFinishMessage)) {
                  //  update the right panel
                  boolean detailsFragmentChanged = false;
                  if (waitedPreview) {
      }
  
      private void startSynchronization() {
-         ContentResolver.cancelSync(null, AccountAuthenticator.AUTHORITY);   // cancel the current synchronizations of any ownCloud account
+         ContentResolver.cancelSync(null, MainApp.getAuthTokenType());   // cancel the current synchronizations of any ownCloud account
          Bundle bundle = new Bundle();
          bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
          ContentResolver.requestSync(
                  getAccount(),
-                 AccountAuthenticator.AUTHORITY, bundle);
+                 MainApp.getAuthTokenType(), bundle);
      }
  
  
          super.onResume();
          Log_OC.e(TAG, "onResume() start");
  
+         FileUploader fileUploader = new FileUploader();
+         FileSyncService fileSyncService = new FileSyncService();
+         
          // Listen for sync messages
-         IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
+         IntentFilter syncIntentFilter = new IntentFilter(fileSyncService.getSyncMessage());
          mSyncBroadcastReceiver = new SyncBroadcastReceiver();
          registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
  
          // Listen for upload messages
-         IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
+         IntentFilter uploadIntentFilter = new IntentFilter(fileUploader.getUploadFinishMessage());
          mUploadFinishReceiver = new UploadFinishReceiver();
          registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
  
          // Listen for download messages
-         IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
-         downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
+         IntentFilter downloadIntentFilter = new IntentFilter(mDownloadAddedMessage);
+         downloadIntentFilter.addAction(mDownloadFinishMessage);
          mDownloadFinishReceiver = new DownloadFinishReceiver();
          registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
      
  
              Log_OC.d(TAG, "sync of account " + accountName + " is in_progress: " + inProgress);
  
 -            if (getAccount() != null && accountName.equals(getAccount().name)) {  
 +            if (getAccount() != null && accountName.equals(getAccount().name)
 +                    && mStorageManager != null
 +                    ) {  
  
                  String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); 
  
                  if ((synchFolderRemotePath != null && currentDir != null && (currentDir.getRemotePath().equals(synchFolderRemotePath)))
                          || fillBlankRoot ) {
                      if (!fillBlankRoot) 
 -                        currentDir = getStorageManager().getFileByPath(synchFolderRemotePath);
 +                        currentDir = mStorageManager.getFileByPath(synchFolderRemotePath);
                      OCFileListFragment fileListFragment = getListOfFilesFragment();
                      if (fileListFragment != null) {
                          fileListFragment.listDirectory(currentDir);
              if (file.isDirectory()) {
                  return file;
              } else if (mStorageManager != null) {
 -                return mStorageManager.getFileById(file.getParentId());
 +                String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
 +                return mStorageManager.getFileByPath(parentPath);
              }
          }
          return null;
@@@ -64,7 -64,6 +64,7 @@@ import com.owncloud.android.ui.activity
  import com.owncloud.android.ui.activity.FileDisplayActivity;
  import com.owncloud.android.ui.dialog.EditNameDialog;
  import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
 +import com.owncloud.android.ui.preview.PreviewImageFragment;
  
  import eu.alefzero.webdav.OnDatatransferProgressListener;
  
@@@ -207,7 -206,8 +207,8 @@@ public class FileDetailFragment extend
      public void onResume() {
          super.onResume();
          mUploadFinishReceiver = new UploadFinishReceiver();
-         IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
+         FileUploader fileUploader = new FileUploader();
+         IntentFilter filter = new IntentFilter(fileUploader.getUploadFinishMessage());
          getActivity().registerReceiver(mUploadFinishReceiver, filter);
  
      }
                          msg.show();
                      }
                      getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done
 +                    
                      updateFileDetails(false, false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
 +                   
 +                    // Force the preview if the file is an image
 +                    if (uploadWasFine && PreviewImageFragment.canBePreviewed(getFile())) {
 +                        ((FileDisplayActivity) mContainerActivity).startImagePreview(getFile());
 +                    } 
                  }
              }
          }
@@@ -82,7 -82,10 +82,10 @@@ public class PreviewImageActivity exten
  
      private boolean mFullScreen;
      
+     private String mDownloadAddedMessage;
+     private String mDownloadFinishMessage;
+     
+     
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          } else {
              mRequestWaitingForBinder = false;
          }
+         
+         FileDownloader downloader = new FileDownloader();
+         mDownloadAddedMessage = downloader.getDownloadAddedMessage();
+         mDownloadFinishMessage= downloader.getDownloadFinishMessage();
      }
  
      private void initViewPager() {
 -        OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
 +        // get parent from path
 +        String parentPath = getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
 +        OCFile parentFolder = mStorageManager.getFileByPath(parentPath);
 +        //OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
          if (parentFolder == null) {
              // should not be necessary
              parentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
                      Log_OC.d(TAG, "Simulating reselection of current page after connection of download binder");
                      onPageSelected(mViewPager.getCurrentItem());
                  }
 -                    
 +
              } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
                  Log_OC.d(TAG, "Upload service connected");
                  mUploaderBinder = (FileUploaderBinder) service;
          super.onResume();
          //Log.e(TAG, "ACTIVITY, ONRESUME");
          mDownloadFinishReceiver = new DownloadFinishReceiver();
-         IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
-         filter.addAction(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
+         
+         IntentFilter filter = new IntentFilter(mDownloadFinishMessage);
+         filter.addAction(mDownloadAddedMessage);
          registerReceiver(mDownloadFinishReceiver, filter);
      }
  
                  boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
                  //boolean isOffscreen =  Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
                  
-                 if (position >= 0 && intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
+                 if (position >= 0 && intent.getAction().equals(mDownloadFinishMessage)) {
                      if (downloadWasFine) {
                          mPreviewImagePagerAdapter.updateFile(position, file);   
                          
              if (!file.isImage()) {
                  throw new IllegalArgumentException("Non-image file passed as argument");
              }
 -            mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
 -            file = mStorageManager.getFileById(file.getFileId()); 
 +            mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());            
 +            
 +            // Update file according to DB file, if it is possible
 +            if (file.getFileId() > DataStorageManager.ROOT_PARENT_ID)            
 +                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