Variable nameing / imports improved, fixed nullpointer on account
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
index 5bf7146..294dbfa 100644 (file)
@@ -23,12 +23,16 @@ import android.accounts.AccountManager;
 import android.app.AlertDialog;\r
 import android.app.AlertDialog.Builder;\r
 import android.app.Dialog;\r
+import android.content.BroadcastReceiver;\r
+import android.content.ContentResolver;\r
+import android.content.Context;\r
 import android.content.DialogInterface;\r
-import android.content.DialogInterface.OnCancelListener;\r
 import android.content.DialogInterface.OnClickListener;\r
 import android.content.Intent;\r
+import android.content.IntentFilter;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.util.Log;\r
 import android.view.View;\r
 import android.view.ViewGroup;\r
 import android.widget.ArrayAdapter;\r
@@ -41,13 +45,15 @@ import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.actionbarsherlock.view.Menu;\r
 import com.actionbarsherlock.view.MenuInflater;\r
 import com.actionbarsherlock.view.MenuItem;\r
+import com.actionbarsherlock.view.Window;\r
 \r
+import eu.alefzero.owncloud.AccountUtils;\r
 import eu.alefzero.owncloud.R;\r
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
-import eu.alefzero.owncloud.authenticator.AuthUtils;\r
 import eu.alefzero.owncloud.datamodel.DataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
+import eu.alefzero.owncloud.syncadapter.FileSyncService;\r
 import eu.alefzero.owncloud.ui.fragment.FileListFragment;\r
 import eu.alefzero.webdav.WebdavClient;\r
 \r
@@ -59,11 +65,14 @@ import eu.alefzero.webdav.WebdavClient;
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
-               OnNavigationListener {\r
+               OnNavigationListener, OnClickListener {\r
        private ArrayAdapter<String> mDirectories;\r
        private DataStorageManager mStorageManager;\r
 \r
-       private static final int DIALOG_CHOOSE_ACCOUNT = 0;\r
+       private SyncBroadcastReceiver  syncBroadcastRevceiver;\r
+       \r
+       private static final int DIALOG_SETUP_ACCOUNT = 0;\r
+       private static final int DIALOG_CREATE_DIR = 1;\r
 \r
        public void pushPath(String path) {\r
                mDirectories.insert(path, 0);\r
@@ -75,56 +84,82 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
        }\r
 \r
        @Override\r
-       protected Dialog onCreateDialog(int id, Bundle args) {\r
-               final AlertDialog.Builder builder = new Builder(this);\r
-               final EditText dirName = new EditText(getBaseContext());\r
-               final Account a = AuthUtils.getCurrentOwnCloudAccount(this);\r
-               builder.setView(dirName);\r
-               builder.setTitle(R.string.uploader_info_dirname);\r
-               dirName.setTextColor(R.color.setup_text_typed);\r
-\r
-               builder.setPositiveButton(R.string.common_ok, new OnClickListener() {\r
-                       public void onClick(DialogInterface dialog, int which) {\r
-                               String s = dirName.getText().toString();\r
-                               if (s.trim().isEmpty()) {\r
-                                       dialog.cancel();\r
-                                       return;\r
-                               }\r
-\r
-                               String path = "";\r
-                               for (int i = mDirectories.getCount() - 2; i >= 0; --i) {\r
-                                       path += "/" + mDirectories.getItem(i);\r
-                               }\r
-                               OCFile parent = mStorageManager.getFileByPath(path + "/");\r
-                               path += s + "/";\r
-                               Thread thread = new Thread(new DirectoryCreator(path, a));\r
-                               thread.start();\r
-                               \r
-                               OCFile new_file = new OCFile(path);\r
-                               new_file.setMimetype("DIR");\r
-                               new_file.setParentId(parent.getParentId());\r
-                               mStorageManager.saveFile(new_file);\r
-\r
-                               dialog.dismiss();\r
-                       }\r
-               });\r
-               builder.setNegativeButton(R.string.common_cancel,\r
-                               new OnClickListener() {\r
-                                       public void onClick(DialogInterface dialog, int which) {\r
-                                               dialog.cancel();\r
-                                       }\r
-                               });\r
-               return builder.create();\r
-       }\r
+  protected Dialog onCreateDialog(int id) {\r
+    Dialog dialog;\r
+    AlertDialog.Builder builder;\r
+    switch(id){\r
+    case DIALOG_SETUP_ACCOUNT:\r
+      builder = new AlertDialog.Builder(this);\r
+      builder.setTitle(R.string.main_tit_accsetup);\r
+      builder.setMessage(R.string.main_wrn_accsetup);\r
+      builder.setCancelable(false);\r
+      builder.setPositiveButton(R.string.common_ok, this);\r
+      builder.setNegativeButton(R.string.common_cancel, this);\r
+      dialog = builder.create();\r
+      break;\r
+    case DIALOG_CREATE_DIR:\r
+    {\r
+      builder = new Builder(this);\r
+      final EditText dirName = new EditText(getBaseContext());\r
+      final Account a = AccountUtils.getCurrentOwnCloudAccount(this);\r
+      builder.setView(dirName);\r
+      builder.setTitle(R.string.uploader_info_dirname);\r
+      dirName.setTextColor(R.color.setup_text_typed);\r
+\r
+      builder.setPositiveButton(R.string.common_ok, new OnClickListener() {\r
+        public void onClick(DialogInterface dialog, int which) {\r
+          String s = dirName.getText().toString();\r
+          if (s.trim().isEmpty()) {\r
+            dialog.cancel();\r
+            return;\r
+          }\r
+\r
+          String path = "";\r
+          for (int i = mDirectories.getCount() - 2; i >= 0; --i) {\r
+            path += "/" + mDirectories.getItem(i);\r
+          }\r
+          OCFile parent = mStorageManager.getFileByPath(path + "/");\r
+          path += s + "/";\r
+          Thread thread = new Thread(new DirectoryCreator(path, a));\r
+          thread.start();\r
+          \r
+          OCFile new_file = new OCFile(path);\r
+          new_file.setMimetype("DIR");\r
+          new_file.setParentId(parent.getParentId());\r
+          mStorageManager.saveFile(new_file);\r
+\r
+          dialog.dismiss();\r
+        }\r
+      });\r
+      builder.setNegativeButton(R.string.common_cancel,\r
+          new OnClickListener() {\r
+            public void onClick(DialogInterface dialog, int which) {\r
+              dialog.cancel();\r
+            }\r
+          });\r
+    }\r
+    default: \r
+      dialog = null;\r
+    }\r
+      \r
+    return dialog;\r
+  }\r
 \r
        @Override\r
        public void onCreate(Bundle savedInstanceState) {\r
                super.onCreate(savedInstanceState);\r
+               if(!accountsAreSetup()){\r
+      showDialog(DIALOG_SETUP_ACCOUNT);\r
+      return;\r
+    }\r
+\r
+               requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  \r
+\r
                mDirectories = new CustomArrayAdapter<String>(this,\r
                                R.layout.sherlock_spinner_dropdown_item);\r
                mDirectories.add("/");\r
                setContentView(R.layout.files);\r
-               mStorageManager = new FileDataStorageManager(AuthUtils.getCurrentOwnCloudAccount(this), getContentResolver());\r
+               mStorageManager = new FileDataStorageManager(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver());\r
                ActionBar action_bar = getSupportActionBar();\r
                action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);\r
                action_bar.setDisplayShowTitleEnabled(false);\r
@@ -141,11 +176,19 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                        break;\r
                }\r
                case R.id.createDirectoryItem: {\r
-                       showDialog(0);\r
+                       showDialog(DIALOG_CREATE_DIR);\r
                        break;\r
                }\r
+               case R.id.startSync: {\r
+                 Bundle bundle = new Bundle();\r
+      bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
+      ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this),\r
+                     "org.owncloud",\r
+                     bundle);\r
+      break;\r
+               }\r
                case android.R.id.home: {\r
-                       navigateUp();\r
+                       onBackPressed();\r
                        break;\r
                }\r
                        \r
@@ -153,69 +196,68 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                return true;\r
        }\r
        \r
-       public void navigateUp(){\r
-               popPath();\r
-               if(mDirectories.getCount() == 0) {\r
-                       Intent intent = new Intent(this, LandingActivity.class);\r
-                       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
-                       startActivity(intent);\r
+       @Override\r
+       public void onBackPressed(){\r
+               if(mDirectories.getCount() == 1) {\r
+                 finish();\r
                        return;\r
                }\r
+               popPath();\r
                ((FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList))\r
                                .onNavigateUp();\r
        }\r
 \r
        @Override\r
-       protected Dialog onCreateDialog(int id) {\r
-               switch (id) {\r
-               case DIALOG_CHOOSE_ACCOUNT:\r
-                       return createChooseAccountDialog();\r
-               default:\r
-                       throw new IllegalArgumentException("Unknown dialog id: " + id);\r
-               }\r
-       }\r
-\r
-       @Override\r
        public boolean onCreateOptionsMenu(Menu menu) {\r
                MenuInflater inflater = getSherlock().getMenuInflater();\r
                inflater.inflate(R.menu.menu, menu);\r
                return true;\r
        }\r
 \r
-       private Dialog createChooseAccountDialog() {\r
-               final AccountManager accMan = AccountManager.get(this);\r
-               CharSequence[] items = new CharSequence[accMan\r
-                               .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];\r
-               int i = 0;\r
-               for (Account a : accMan\r
-                               .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {\r
-                       items[i++] = a.name;\r
-               }\r
-\r
-               AlertDialog.Builder builder = new AlertDialog.Builder(this);\r
-               builder.setTitle(R.string.common_choose_account);\r
-               builder.setCancelable(true);\r
-               builder.setItems(items, new DialogInterface.OnClickListener() {\r
-                       public void onClick(DialogInterface dialog, int item) {\r
-                               // mAccount =\r
-                               // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];\r
-                               dialog.dismiss();\r
-                       }\r
-               });\r
-               builder.setOnCancelListener(new OnCancelListener() {\r
-                       public void onCancel(DialogInterface dialog) {\r
-                               FileDisplayActivity.this.finish();\r
-                       }\r
-               });\r
-               AlertDialog alert = builder.create();\r
-               return alert;\r
+        @Override\r
+         protected void onRestoreInstanceState(Bundle savedInstanceState) {\r
+           super.onRestoreInstanceState(savedInstanceState);\r
+           // Check, if there are ownCloud accounts\r
+           if(!accountsAreSetup()){\r
+             showDialog(DIALOG_SETUP_ACCOUNT);\r
+           }\r
+         }\r
+        \r
+        @Override\r
+         protected void onStart() {\r
+           super.onStart();\r
+           // Check, if there are ownCloud accounts\r
+           if(!accountsAreSetup()){\r
+             showDialog(DIALOG_SETUP_ACCOUNT);\r
+           }\r
+        }\r
+           \r
+        @Override\r
+       protected void onResume() {\r
+         super.onResume();\r
+         if(!accountsAreSetup()){\r
+      showDialog(DIALOG_SETUP_ACCOUNT);\r
+      return;\r
+    }\r
+          IntentFilter syncMessageIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);\r
+          syncBroadcastRevceiver = new  SyncBroadcastReceiver();\r
+          registerReceiver(syncBroadcastRevceiver, syncMessageIntentFilter);\r
+          setProgressBarIndeterminateVisibility(false);\r
        }\r
-\r
+           \r
+        @Override\r
+       protected void onPause() {\r
+         super.onPause();\r
+         if(syncBroadcastRevceiver != null){\r
+                 unregisterReceiver(syncBroadcastRevceiver);  \r
+         }\r
+       }\r
+        \r
        @Override\r
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {\r
                int i = itemPosition;\r
                while (i-- != 0) {\r
-                       navigateUp();\r
+                       onBackPressed();\r
                }\r
                return true;\r
        }\r
@@ -279,4 +321,53 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
                \r
        }\r
-}
\ No newline at end of file
+\r
+        public void onClick(DialogInterface dialog, int which) {\r
+           // In any case - we won't need it anymore\r
+           dialog.dismiss();\r
+           switch(which){\r
+           case DialogInterface.BUTTON_POSITIVE:\r
+             Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");\r
+             intent.putExtra("authorities",\r
+                 new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });\r
+             startActivity(intent);\r
+             break;\r
+           case DialogInterface.BUTTON_NEGATIVE:\r
+             finish();\r
+           }\r
+           \r
+         }\r
+       \r
+       /**\r
+   * Checks, whether or not there are any ownCloud accounts \r
+   * setup. \r
+   *  \r
+   * @return true, if there is at least one account.\r
+   */\r
+  private boolean accountsAreSetup() {\r
+    AccountManager accMan = AccountManager.get(this);\r
+    Account[] accounts = accMan\r
+        .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); \r
+    return accounts.length > 0;\r
+  }\r
+  \r
+  private class SyncBroadcastReceiver extends BroadcastReceiver {\r
+         /**\r
+          * {@link BroadcastReceiver} to enable syncing feedback in UI\r
+          */\r
+    @Override\r
+    public void onReceive(Context context, Intent intent) {\r
+      boolean inProgress = intent.getBooleanExtra(FileSyncService.IN_PROGRESS, false);\r
+      String account_name = intent.getStringExtra(FileSyncService.ACCOUNT_NAME);\r
+      Log.d("FileDisplay", "sync of account " + account_name + " is in_progress: " + inProgress);\r
+      setProgressBarIndeterminateVisibility(inProgress);\r
+      if (!inProgress) {\r
+        FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+        if (fileListFramgent != null)\r
+          fileListFramgent.populateFileList();\r
+      }\r
+    }\r
+    \r
+  }\r
+  \r
+}