merge fix
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
index cac6fea..1db5246 100644 (file)
@@ -23,12 +23,18 @@ 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.database.Cursor;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.provider.MediaStore;\r
+import android.util.Log;\r
 import android.view.View;\r
 import android.view.ViewGroup;\r
 import android.widget.ArrayAdapter;\r
@@ -41,6 +47,7 @@ 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
@@ -48,6 +55,7 @@ import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
 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
@@ -63,8 +71,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
        private ArrayAdapter<String> mDirectories;\r
        private DataStorageManager mStorageManager;\r
 \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
+       private static final int REQUEST_ACCOUNT_SETUP = 0;\r
+       private static final int ACTION_SELECT_FILE = 1;\r
 \r
        public void pushPath(String path) {\r
                mDirectories.insert(path, 0);\r
@@ -85,8 +98,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
       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
+      builder.setPositiveButton(android.R.string.ok, this);\r
+      builder.setNegativeButton(android.R.string.cancel, this);\r
       dialog = builder.create();\r
       break;\r
     case DIALOG_CREATE_DIR:\r
@@ -96,12 +109,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
       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
+      int typed_color = getResources().getColor(R.color.setup_text_typed);\r
+      dirName.setTextColor(typed_color);\r
 \r
-      builder.setPositiveButton(R.string.common_ok, new OnClickListener() {\r
+      builder.setPositiveButton(android.R.string.ok, new OnClickListener() {\r
         public void onClick(DialogInterface dialog, int which) {\r
           String s = dirName.getText().toString();\r
-          if (s.trim().isEmpty()) {\r
+          if (s.trim().length() == 0) {\r
             dialog.cancel();\r
             return;\r
           }\r
@@ -129,6 +143,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
               dialog.cancel();\r
             }\r
           });\r
+      dialog = builder.create();\r
+      break;\r
     }\r
     default: \r
       dialog = null;\r
@@ -140,25 +156,19 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
        @Override\r
        public void onCreate(Bundle savedInstanceState) {\r
                super.onCreate(savedInstanceState);\r
+\r
                if(!accountsAreSetup()){\r
       showDialog(DIALOG_SETUP_ACCOUNT);\r
       return;\r
     }\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(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver());\r
-               ActionBar action_bar = getSupportActionBar();\r
-               action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);\r
-               action_bar.setDisplayShowTitleEnabled(false);\r
-               action_bar.setListNavigationCallbacks(mDirectories, this);\r
-               action_bar.setDisplayHomeAsUpEnabled(true);\r
+               \r
+               requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
+    setProgressBarIndeterminateVisibility(false);\r
        }\r
 \r
        @Override\r
        public boolean onOptionsItemSelected(MenuItem item) {\r
+         boolean retval = true;\r
                switch (item.getItemId()) {\r
                case R.id.settingsItem: {\r
                        Intent i = new Intent(this, Preferences.class);\r
@@ -169,13 +179,31 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                        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 R.id.action_upload: {\r
+                 Intent action = new Intent(Intent.ACTION_GET_CONTENT);  \r
+                 action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);  \r
+                 startActivityForResult(Intent.createChooser(action, "Upload file from..."), ACTION_SELECT_FILE);\r
+                 break;\r
+               }\r
+                 \r
                case android.R.id.home: {\r
-                       onBackPressed();\r
+                 Intent i = new Intent(this, AccountSelectActivity.class);\r
+                 startActivity(i);\r
+                 finish();\r
                        break;\r
                }\r
-                       \r
+                       default:\r
+                         retval = false;\r
                }\r
-               return true;\r
+               return retval;\r
        }\r
        \r
        @Override\r
@@ -204,24 +232,85 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
              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
            \r
         @Override\r
        protected void onResume() {\r
          super.onResume();\r
          if(!accountsAreSetup()){\r
       showDialog(DIALOG_SETUP_ACCOUNT);\r
+      return;\r
     }\r
+         \r
+          IntentFilter f = new IntentFilter(FileSyncService.SYNC_MESSAGE);\r
+          b = new  BR();\r
+          registerReceiver(b, f);\r
+          if (getSupportFragmentManager().findFragmentById(R.id.fileList) == null)\r
+            setContentView(R.layout.files);\r
+          \r
+          mDirectories = new CustomArrayAdapter<String>(this,\r
+               R.layout.sherlock_spinner_dropdown_item);\r
+           mDirectories.add("/");\r
+           \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
+           action_bar.setListNavigationCallbacks(mDirectories, this);\r
+           action_bar.setDisplayHomeAsUpEnabled(true);\r
        }\r
            \r
+        public void onActivityResult(int requestCode, int resultCode, Intent data) {\r
+     if (resultCode == RESULT_OK) {\r
+         if (requestCode == ACTION_SELECT_FILE) {\r
+             Uri selectedImageUri = data.getData();\r
+\r
+             String filemanagerstring = selectedImageUri.getPath();\r
+\r
+             String selectedImagePath = getPath(selectedImageUri);\r
+\r
+             //DEBUG PURPOSE - you can delete this if you want\r
+             if(selectedImagePath!=null)\r
+                 System.out.println(selectedImagePath);\r
+             else System.out.println("selectedImagePath is null");\r
+             if(filemanagerstring!=null)\r
+                 System.out.println(filemanagerstring);\r
+             else System.out.println("filemanagerstring is null");\r
+\r
+             //NOW WE HAVE OUR WANTED STRING\r
+             if(selectedImagePath!=null)\r
+                 System.out.println("selectedImagePath is the right one for you!");\r
+             else\r
+                 System.out.println("filemanagerstring is the right one for you!");\r
+         }\r
+     }\r
+        }\r
+        \r
+     public String getPath(Uri uri) {\r
+       String[] projection = { MediaStore.Images.Media.DATA };\r
+       Cursor cursor = managedQuery(uri, projection, null, null, null);\r
+       if(cursor!=null)\r
+       {\r
+           //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL\r
+           //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA\r
+           int column_index = cursor\r
+           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);\r
+           cursor.moveToFirst();\r
+           return cursor.getString(column_index);\r
+       }\r
+       else return null;\r
+   }\r
+     \r
+        @Override\r
+       protected void onPause() {\r
+         super.onPause();\r
+         if (b != null) {\r
+           unregisterReceiver(b);\r
+           b = null;\r
+         }\r
+         \r
+       }\r
+        \r
        @Override\r
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {\r
                int i = itemPosition;\r
@@ -290,7 +379,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
                \r
        }\r
-\r
+       \r
         public void onClick(DialogInterface dialog, int which) {\r
            // In any case - we won't need it anymore\r
            dialog.dismiss();\r
@@ -320,5 +409,23 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     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
 }