X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/63c213cbea5177061793fd19ad6143384982b335..390949b8323633b26d56b3a837e83941cbc24b4b:/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index a5437773..1db5246e 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -23,12 +23,18 @@ import android.accounts.AccountManager; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; +import android.content.BroadcastReceiver; +import android.content.ContentResolver; +import android.content.Context; import android.content.DialogInterface; -import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.Intent; +import android.content.IntentFilter; +import android.database.Cursor; import android.net.Uri; import android.os.Bundle; +import android.provider.MediaStore; +import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; @@ -41,13 +47,15 @@ import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; +import com.actionbarsherlock.view.Window; +import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.authenticator.AuthUtils; import eu.alefzero.owncloud.datamodel.DataStorageManager; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.syncadapter.FileSyncService; import eu.alefzero.owncloud.ui.fragment.FileListFragment; import eu.alefzero.webdav.WebdavClient; @@ -59,11 +67,17 @@ import eu.alefzero.webdav.WebdavClient; */ public class FileDisplayActivity extends SherlockFragmentActivity implements - OnNavigationListener { + OnNavigationListener, OnClickListener { private ArrayAdapter mDirectories; private DataStorageManager mStorageManager; - private static final int DIALOG_CHOOSE_ACCOUNT = 0; + private SyncBroadcastReceiver syncBroadcastRevceiver; + + private static final int DIALOG_SETUP_ACCOUNT = 0; + private static final int DIALOG_CREATE_DIR = 1; + + private static final int REQUEST_ACCOUNT_SETUP = 0; + private static final int ACTION_SELECT_FILE = 1; public void pushPath(String path) { mDirectories.insert(path, 0); @@ -75,65 +89,86 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } @Override - protected Dialog onCreateDialog(int id, Bundle args) { - final AlertDialog.Builder builder = new Builder(this); - final EditText dirName = new EditText(getBaseContext()); - final Account a = AuthUtils.getCurrentOwnCloudAccount(this); - builder.setView(dirName); - builder.setTitle(R.string.uploader_info_dirname); - dirName.setTextColor(R.color.setup_text_typed); - - builder.setPositiveButton(R.string.common_ok, new OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - String s = dirName.getText().toString(); - if (s.trim().isEmpty()) { - dialog.cancel(); - return; - } - - String path = ""; - for (int i = mDirectories.getCount() - 2; i >= 0; --i) { - path += "/" + mDirectories.getItem(i); - } - OCFile parent = mStorageManager.getFileByPath(path + "/"); - path += s + "/"; - Thread thread = new Thread(new DirectoryCreator(path, a)); - thread.start(); - - OCFile new_file = new OCFile(path); - new_file.setMimetype("DIR"); - new_file.setParentId(parent.getParentId()); - mStorageManager.saveFile(new_file); - - dialog.dismiss(); - } - }); - builder.setNegativeButton(R.string.common_cancel, - new OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } - }); - return builder.create(); - } + protected Dialog onCreateDialog(int id) { + Dialog dialog; + AlertDialog.Builder builder; + switch(id){ + case DIALOG_SETUP_ACCOUNT: + builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.main_tit_accsetup); + builder.setMessage(R.string.main_wrn_accsetup); + builder.setCancelable(false); + builder.setPositiveButton(android.R.string.ok, this); + builder.setNegativeButton(android.R.string.cancel, this); + dialog = builder.create(); + break; + case DIALOG_CREATE_DIR: + { + builder = new Builder(this); + final EditText dirName = new EditText(getBaseContext()); + final Account a = AccountUtils.getCurrentOwnCloudAccount(this); + builder.setView(dirName); + builder.setTitle(R.string.uploader_info_dirname); + int typed_color = getResources().getColor(R.color.setup_text_typed); + dirName.setTextColor(typed_color); + + builder.setPositiveButton(android.R.string.ok, new OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + String s = dirName.getText().toString(); + if (s.trim().length() == 0) { + dialog.cancel(); + return; + } + + String path = ""; + for (int i = mDirectories.getCount() - 2; i >= 0; --i) { + path += "/" + mDirectories.getItem(i); + } + OCFile parent = mStorageManager.getFileByPath(path + "/"); + path += s + "/"; + Thread thread = new Thread(new DirectoryCreator(path, a)); + thread.start(); + + OCFile new_file = new OCFile(path); + new_file.setMimetype("DIR"); + new_file.setParentId(parent.getParentId()); + mStorageManager.saveFile(new_file); + + dialog.dismiss(); + } + }); + builder.setNegativeButton(R.string.common_cancel, + new OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + dialog = builder.create(); + break; + } + default: + dialog = null; + } + + return dialog; + } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mDirectories = new CustomArrayAdapter(this, - R.layout.sherlock_spinner_dropdown_item); - mDirectories.add("/"); - setContentView(R.layout.files); - mStorageManager = new FileDataStorageManager(AuthUtils.getCurrentOwnCloudAccount(this), getContentResolver()); - ActionBar action_bar = getSupportActionBar(); - action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); - action_bar.setDisplayShowTitleEnabled(false); - action_bar.setListNavigationCallbacks(mDirectories, this); - action_bar.setDisplayHomeAsUpEnabled(true); + + if(!accountsAreSetup()){ + showDialog(DIALOG_SETUP_ACCOUNT); + return; + } + + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setProgressBarIndeterminateVisibility(false); } @Override public boolean onOptionsItemSelected(MenuItem item) { + boolean retval = true; switch (item.getItemId()) { case R.id.settingsItem: { Intent i = new Intent(this, Preferences.class); @@ -141,16 +176,34 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements break; } case R.id.createDirectoryItem: { - showDialog(0); + showDialog(DIALOG_CREATE_DIR); break; } + case R.id.startSync: { + Bundle bundle = new Bundle(); + bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + ContentResolver.requestSync(AccountUtils.getCurrentOwnCloudAccount(this), + "org.owncloud", + bundle); + break; + } + case R.id.action_upload: { + Intent action = new Intent(Intent.ACTION_GET_CONTENT); + action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE); + startActivityForResult(Intent.createChooser(action, "Upload file from..."), ACTION_SELECT_FILE); + break; + } + case android.R.id.home: { - onBackPressed(); + Intent i = new Intent(this, AccountSelectActivity.class); + startActivity(i); + finish(); break; } - + default: + retval = false; } - return true; + return retval; } @Override @@ -165,51 +218,99 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } @Override - protected Dialog onCreateDialog(int id) { - switch (id) { - case DIALOG_CHOOSE_ACCOUNT: - return createChooseAccountDialog(); - default: - throw new IllegalArgumentException("Unknown dialog id: " + id); - } - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } - private Dialog createChooseAccountDialog() { - final AccountManager accMan = AccountManager.get(this); - CharSequence[] items = new CharSequence[accMan - .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length]; - int i = 0; - for (Account a : accMan - .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) { - items[i++] = a.name; - } - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.common_choose_account); - builder.setCancelable(true); - builder.setItems(items, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int item) { - // mAccount = - // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item]; - dialog.dismiss(); - } - }); - builder.setOnCancelListener(new OnCancelListener() { - public void onCancel(DialogInterface dialog) { - FileDisplayActivity.this.finish(); - } - }); - AlertDialog alert = builder.create(); - return alert; + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + // Check, if there are ownCloud accounts + if(!accountsAreSetup()){ + showDialog(DIALOG_SETUP_ACCOUNT); + } + } + + + @Override + protected void onResume() { + super.onResume(); + if(!accountsAreSetup()){ + showDialog(DIALOG_SETUP_ACCOUNT); + return; + } + + IntentFilter f = new IntentFilter(FileSyncService.SYNC_MESSAGE); + b = new BR(); + registerReceiver(b, f); + if (getSupportFragmentManager().findFragmentById(R.id.fileList) == null) + setContentView(R.layout.files); + + mDirectories = new CustomArrayAdapter(this, + R.layout.sherlock_spinner_dropdown_item); + mDirectories.add("/"); + + mStorageManager = new FileDataStorageManager(AccountUtils.getCurrentOwnCloudAccount(this), getContentResolver()); + ActionBar action_bar = getSupportActionBar(); + action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + action_bar.setDisplayShowTitleEnabled(false); + action_bar.setListNavigationCallbacks(mDirectories, this); + action_bar.setDisplayHomeAsUpEnabled(true); } - + + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == RESULT_OK) { + if (requestCode == ACTION_SELECT_FILE) { + Uri selectedImageUri = data.getData(); + + String filemanagerstring = selectedImageUri.getPath(); + + String selectedImagePath = getPath(selectedImageUri); + + //DEBUG PURPOSE - you can delete this if you want + if(selectedImagePath!=null) + System.out.println(selectedImagePath); + else System.out.println("selectedImagePath is null"); + if(filemanagerstring!=null) + System.out.println(filemanagerstring); + else System.out.println("filemanagerstring is null"); + + //NOW WE HAVE OUR WANTED STRING + if(selectedImagePath!=null) + System.out.println("selectedImagePath is the right one for you!"); + else + System.out.println("filemanagerstring is the right one for you!"); + } + } + } + + public String getPath(Uri uri) { + String[] projection = { MediaStore.Images.Media.DATA }; + Cursor cursor = managedQuery(uri, projection, null, null, null); + if(cursor!=null) + { + //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL + //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA + int column_index = cursor + .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + cursor.moveToFirst(); + return cursor.getString(column_index); + } + else return null; + } + + @Override + protected void onPause() { + super.onPause(); + if (b != null) { + unregisterReceiver(b); + b = null; + } + + } + @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { int i = itemPosition; @@ -278,4 +379,53 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } + + public void onClick(DialogInterface dialog, int which) { + // In any case - we won't need it anymore + dialog.dismiss(); + switch(which){ + case DialogInterface.BUTTON_POSITIVE: + Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); + intent.putExtra("authorities", + new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); + startActivity(intent); + break; + case DialogInterface.BUTTON_NEGATIVE: + finish(); + } + + } + + /** + * Checks, whether or not there are any ownCloud accounts + * setup. + * + * @return true, if there is at least one account. + */ + private boolean accountsAreSetup() { + AccountManager accMan = AccountManager.get(this); + Account[] accounts = accMan + .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); + return accounts.length > 0; + } + + private class SyncBroadcastReceiver extends BroadcastReceiver { + /** + * {@link BroadcastReceiver} to enable syncing feedback in UI + */ + @Override + public void onReceive(Context context, Intent intent) { + boolean inProgress = intent.getBooleanExtra(FileSyncService.IN_PROGRESS, false); + String account_name = intent.getStringExtra(FileSyncService.ACCOUNT_NAME); + Log.d("FileDisplay", "sync of account " + account_name + " is in_progress: " + inProgress); + setProgressBarIndeterminateVisibility(inProgress); + if (!inProgress) { + FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); + if (fileListFramgent != null) + fileListFramgent.populateFileList(); + } + } + + } + }