From f19f366a36aa0185aecff55644043268dcac8320 Mon Sep 17 00:00:00 2001 From: Konrad Renner Date: Thu, 10 Sep 2015 21:25:41 +0200 Subject: [PATCH] solved GitHub issue 65 --- .../kolabnotes/android/DetailActivity.java | 29 ++++- .../fragment/ChooseAccountDialogFragment.java | 105 ++++++++++++++++++ .../android/fragment/DetailFragment.java | 15 ++- .../fragment/OnAccountChooseListener.java | 10 ++ .../res/layout/fragment_choose_account.xml | 43 +++++++ app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 8 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/kore/kolabnotes/android/fragment/ChooseAccountDialogFragment.java create mode 100644 app/src/main/java/org/kore/kolabnotes/android/fragment/OnAccountChooseListener.java create mode 100644 app/src/main/res/layout/fragment_choose_account.xml diff --git a/app/src/main/java/org/kore/kolabnotes/android/DetailActivity.java b/app/src/main/java/org/kore/kolabnotes/android/DetailActivity.java index e021b95..e84b50e 100755 --- a/app/src/main/java/org/kore/kolabnotes/android/DetailActivity.java +++ b/app/src/main/java/org/kore/kolabnotes/android/DetailActivity.java @@ -1,23 +1,29 @@ package org.kore.kolabnotes.android; +import android.app.FragmentManager; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.widget.Toast; +import org.kore.kolabnotes.android.content.AccountIdentifier; +import org.kore.kolabnotes.android.fragment.ChooseAccountDialogFragment; import org.kore.kolabnotes.android.fragment.DetailFragment; +import org.kore.kolabnotes.android.fragment.OnAccountChooseListener; import org.kore.kolabnotes.android.fragment.OnFragmentCallback; -public class DetailActivity extends AppCompatActivity implements OnFragmentCallback { +public class DetailActivity extends AppCompatActivity implements OnFragmentCallback, OnAccountChooseListener { private DetailFragment detailFragment; + private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + toolbar = (Toolbar) findViewById(R.id.toolbar); if(toolbar != null){ toolbar.setTitle(""); } @@ -30,6 +36,25 @@ protected void onCreate(Bundle savedInstanceState) { detailFragment.setStartNotebook(notebook); detailFragment.setStartUid(uid); + + String action = startIntent.getAction(); + if (Intent.ACTION_SEND.equals(action)) { + showAccountChooseDialog(); + } + } + + private void showAccountChooseDialog() { + FragmentManager fm = getFragmentManager(); + ChooseAccountDialogFragment chooseAccountDialog = new ChooseAccountDialogFragment(); + chooseAccountDialog.show(fm, "fragment_choose_account"); + } + + @Override + public void onAccountElected(String name,AccountIdentifier accountIdentifier){ + detailFragment.resetSpinner(); + if(toolbar != null){ + toolbar.setTitle(name); + } } @Override diff --git a/app/src/main/java/org/kore/kolabnotes/android/fragment/ChooseAccountDialogFragment.java b/app/src/main/java/org/kore/kolabnotes/android/fragment/ChooseAccountDialogFragment.java new file mode 100644 index 0000000..508ac29 --- /dev/null +++ b/app/src/main/java/org/kore/kolabnotes/android/fragment/ChooseAccountDialogFragment.java @@ -0,0 +1,105 @@ +package org.kore.kolabnotes.android.fragment; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.DialogFragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; + +import org.kore.kolabnotes.android.R; +import org.kore.kolabnotes.android.Utils; +import org.kore.kolabnotes.android.content.AccountIdentifier; +import org.kore.kolabnotes.android.content.ActiveAccount; +import org.kore.kolabnotes.android.content.ActiveAccountRepository; +import org.kore.kolabnotes.android.security.AuthenticatorActivity; + +import java.util.Arrays; + +/** + * Created by koni on 10.09.15. + */ +public class ChooseAccountDialogFragment extends DialogFragment implements View.OnClickListener{ + + private ActiveAccountRepository activeAccountRepository; + + private Spinner accountSpinner; + private Button selectButton; + private Button cancelButton; + private AccountManager mAccountManager; + private String localAccountName; + + public ChooseAccountDialogFragment() { + // Empty constructor required for DialogFragment + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_choose_account, container); + + activeAccountRepository = new ActiveAccountRepository(getActivity()); + + accountSpinner = (Spinner) view.findViewById(R.id.spinner_account); + selectButton = (Button) view.findViewById(R.id.select_button); + cancelButton = (Button) view.findViewById(R.id.cancel_button); + selectButton.setOnClickListener(this); + cancelButton.setOnClickListener(this); + localAccountName = getResources().getString(R.string.drawer_account_local); + getDialog().setTitle(R.string.account_choose_title); + + mAccountManager = AccountManager.get(getActivity()); + + initAccountSpinner(); + return view; + } + + @Override + public void onClick(View view) { + if(view.getId() == R.id.select_button) { + OnAccountChooseListener listener = (OnAccountChooseListener) getActivity(); + + String name = accountSpinner.getSelectedItem().toString(); + + final AccountIdentifier selectedAccount = Utils.getAccountIdentifierWithName(getActivity(), name); + + activeAccountRepository.switchAccount(selectedAccount.getAccount(), selectedAccount.getRootFolder()); + + listener.onAccountElected(name, selectedAccount); + } + this.dismiss(); + } + + void initAccountSpinner(){ + Account[] accounts = mAccountManager.getAccountsByType(AuthenticatorActivity.ARG_ACCOUNT_TYPE); + + String[] accountNames = new String[accounts.length+1]; + + accountNames[0] = localAccountName; + int selection = 0; + + final ActiveAccount activeAccount = activeAccountRepository.getActiveAccount(); + + for(int i=0; i< accounts.length;i++){ + accountNames[i+1] = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_ACCOUNT_NAME); + + String folder = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_ROOT_FOLDER); + String email = mAccountManager.getUserData(accounts[i],AuthenticatorActivity.KEY_EMAIL); + + if(activeAccount.getAccount().equals(email) && activeAccount.getRootFolder().equals(folder)){ + selection = i+1; + } + } + + Arrays.sort(accountNames, 1, accountNames.length); + + ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.widget_config_spinner_item,accountNames); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + accountSpinner.setAdapter(adapter); + accountSpinner.setSelection(selection); + } +} diff --git a/app/src/main/java/org/kore/kolabnotes/android/fragment/DetailFragment.java b/app/src/main/java/org/kore/kolabnotes/android/fragment/DetailFragment.java index 597eb20..3769d32 100644 --- a/app/src/main/java/org/kore/kolabnotes/android/fragment/DetailFragment.java +++ b/app/src/main/java/org/kore/kolabnotes/android/fragment/DetailFragment.java @@ -316,7 +316,7 @@ void setNotebook(ActiveAccount activeAccount,String uid, boolean setGivenNoteboo } } } - + void initEditor(){ editor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() { @Override @@ -1021,10 +1021,17 @@ public void onClick(DialogInterface dialog, int which) { } } - void initSpinner(){ + public void resetSpinner(){ + Spinner spinner = initSpinner(); + spinner.setSelection(0); + } + + Spinner initSpinner(){ Spinner spinner = (Spinner) activity.findViewById(R.id.spinner_notebook); - List notebooks = notebookRepository.getAll( activeAccountRepository.getActiveAccount().getAccount(), activeAccountRepository.getActiveAccount().getRootFolder()); + final ActiveAccount activeAccount = activeAccountRepository.getActiveAccount(); + + List notebooks = notebookRepository.getAll(activeAccount.getAccount(), activeAccount.getRootFolder()); String[] notebookArr = new String[notebooks.size()]; @@ -1037,6 +1044,8 @@ void initSpinner(){ ArrayAdapter adapter = new ArrayAdapter(activity,R.layout.notebook_spinner_item,notebookArr); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); + + return spinner; } @Override diff --git a/app/src/main/java/org/kore/kolabnotes/android/fragment/OnAccountChooseListener.java b/app/src/main/java/org/kore/kolabnotes/android/fragment/OnAccountChooseListener.java new file mode 100644 index 0000000..f05b56a --- /dev/null +++ b/app/src/main/java/org/kore/kolabnotes/android/fragment/OnAccountChooseListener.java @@ -0,0 +1,10 @@ +package org.kore.kolabnotes.android.fragment; + +import org.kore.kolabnotes.android.content.AccountIdentifier; + +/** + * Created by koni on 10.09.15. + */ +public interface OnAccountChooseListener { + void onAccountElected(String name, AccountIdentifier accountIdentifier); +} diff --git a/app/src/main/res/layout/fragment_choose_account.xml b/app/src/main/res/layout/fragment_choose_account.xml new file mode 100644 index 0000000..3eea097 --- /dev/null +++ b/app/src/main/res/layout/fragment_choose_account.xml @@ -0,0 +1,43 @@ + + + + + + + + + +