+ // Listen for sync messages\r
+ IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);\r
+ syncBroadcastRevceiver = new SyncBroadcastReceiver();\r
+ registerReceiver(syncBroadcastRevceiver, syncIntentFilter);\r
+ \r
+ // Storage manager initialization\r
+ mStorageManager = new FileDataStorageManager(\r
+ AccountUtils.getCurrentOwnCloudAccount(this),\r
+ getContentResolver());\r
+ \r
+ // File list\r
+ mFileList = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+ \r
+ // Figure out what directory to list. \r
+ // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)\r
+ if(getIntent().hasExtra(FileDetailFragment.EXTRA_FILE)){\r
+ mCurrentDir = (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
+ if(!mCurrentDir.isDirectory()){\r
+ mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId());\r
+ }\r
+ \r
+ // Clear intent extra, so rotating the screen will not return us to this directory\r
+ getIntent().removeExtra(FileDetailFragment.EXTRA_FILE);\r
+ } \r
+ \r
+ // Drop-Down navigation and file list restore\r
+ mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);\r
+ \r
+ \r
+ // Given the case we have a file to display:\r
+ if(mCurrentDir != null){\r
+ ArrayList<OCFile> files = new ArrayList<OCFile>();\r
+ OCFile currFile = mCurrentDir;\r
+ while(currFile != null){\r
+ files.add(currFile);\r
+ currFile = mStorageManager.getFileById(currFile.getParentId());\r
+ }\r
+ \r
+ // Insert in mDirs\r
+ mDirs = new String[files.size()];\r
+ for(int i = files.size() - 1; i >= 0; i--){\r
+ mDirs[i] = files.get(i).getFileName();\r
+ }\r
+ }\r
+ \r
+ if (mDirs != null) {\r
+ for (String s : mDirs)\r
+ mDirectories.add(s);\r
+ } else {\r
+ mDirectories.add("/");\r
+ }\r
+ \r
+ // Actionbar setup\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
+ // List dir here\r
+ mFileList.listDirectory(mCurrentDir);\r
+ }\r
+\r
+ @Override\r
+ protected void onPause() {\r
+ super.onPause();\r
+ if (syncBroadcastRevceiver != null) {\r
+ unregisterReceiver(syncBroadcastRevceiver);\r
+ syncBroadcastRevceiver = null;\r
+ }\r
+ \r
+ }\r
+\r
+ @Override\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(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
+ builder = new Builder(this);\r
+ final EditText dirNameInput = new EditText(getBaseContext());\r
+ final Account a = AccountUtils.getCurrentOwnCloudAccount(this);\r
+ builder.setView(dirNameInput);\r
+ builder.setTitle(R.string.uploader_info_dirname);\r
+ int typed_color = getResources().getColor(R.color.setup_text_typed);\r
+ dirNameInput.setTextColor(typed_color);\r
+ \r
+ builder.setPositiveButton(android.R.string.ok,\r
+ new OnClickListener() {\r
+ public void onClick(DialogInterface dialog, int which) {\r
+ String directoryName = dirNameInput.getText().toString();\r
+ if (directoryName.trim().length() == 0) {\r
+ dialog.cancel();\r
+ return;\r
+ }\r
+ \r
+ // Figure out the path where the dir needs to be created\r
+ String path = mCurrentDir.getRemotePath();\r
+ \r
+ // Create directory\r
+ path += directoryName + "/";\r
+ Thread thread = new Thread(new DirectoryCreator(\r
+ path, a));\r
+ thread.start();\r
+ \r
+ // Save new directory in local database\r
+ OCFile newDir = new OCFile(path);\r
+ newDir.setMimetype("DIR");\r
+ newDir.setParentId(mCurrentDir.getFileId());\r
+ mStorageManager.saveFile(newDir);\r
+ \r
+ // Display the new folder right away\r
+ dialog.dismiss();\r
+ mFileList.listDirectory(mCurrentDir);\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
+ dialog = builder.create();\r
+ break;\r
+ }\r
+ default:\r
+ dialog = null;\r
+ }\r
+ \r
+ return dialog;\r
+ }\r
+\r
+ \r
+ /**\r
+ * Responds to the "There are no ownCloud Accounts setup" dialog\r
+ * TODO: Dialog is 100% useless -> Remove\r
+ */\r
+ @Override\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
+ * Translates a content URI of an image to a physical path\r
+ * on the disk\r
+ * @param uri The URI to resolve\r
+ * @return The path to the image or null if it could not be found\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
+ int column_index = cursor\r
+ .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);\r
+ cursor.moveToFirst();\r
+ return cursor.getString(column_index);\r
+ } \r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Pushes a directory to the drop down list\r
+ * @param directory to push\r
+ * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.\r
+ */\r
+ public void pushDirname(OCFile directory) {\r
+ if(!directory.isDirectory()){\r
+ throw new IllegalArgumentException("Only directories may be pushed!");\r
+ }\r
+ mDirectories.insert(directory.getFileName(), 0);\r
+ mCurrentDir = directory;\r
+ }\r
+\r
+ /**\r
+ * Pops a directory name from the drop down list\r
+ * @return True, unless the stack is empty\r
+ */\r
+ public boolean popDirname() {\r
+ mDirectories.remove(mDirectories.getItem(0));\r
+ return !mDirectories.isEmpty();\r
+ }\r
+\r
+ /**\r
+ * Checks, whether or not there are any ownCloud accounts 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 DirectoryCreator implements Runnable {\r
+ private String mTargetPath;\r
+ private Account mAccount;\r
+ private AccountManager mAm;\r
+ \r
+ public DirectoryCreator(String targetPath, Account account) {\r
+ mTargetPath = targetPath;\r
+ mAccount = account;\r
+ mAm = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
+ }\r
+ \r
+ @Override\r
+ public void run() {\r
+ WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(\r
+ mAccount, AccountAuthenticator.KEY_OC_URL)));\r
+ \r
+ String username = mAccount.name.substring(0,\r
+ mAccount.name.lastIndexOf('@'));\r
+ String password = mAm.getPassword(mAccount);\r
+ \r
+ wdc.setCredentials(username, password);\r
+ wdc.allowUnsignedCertificates();\r
+ wdc.createDirectory(mTargetPath);\r
+ }\r
+ \r
+ }\r
+\r
+ // Custom array adapter to override text colors\r
+ private class CustomArrayAdapter<T> extends ArrayAdapter<T> {\r
+ \r
+ public CustomArrayAdapter(FileDisplayActivity ctx, int view) {\r
+ super(ctx, view);\r
+ }\r
+ \r
+ public View getView(int position, View convertView, ViewGroup parent) {\r