-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de6adf8
commit f19f366
Showing
8 changed files
with
203 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/org/kore/kolabnotes/android/fragment/ChooseAccountDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(),R.layout.widget_config_spinner_item,accountNames); | ||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
accountSpinner.setAdapter(adapter); | ||
accountSpinner.setSelection(selection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/java/org/kore/kolabnotes/android/fragment/OnAccountChooseListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/choose_account_fragment" | ||
android:layout_width="wrap_content" android:layout_height="wrap_content" | ||
android:layout_gravity="center" android:orientation="vertical" | ||
android:background="@color/background_material_dark"> | ||
|
||
<Spinner | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/spinner_account" | ||
android:spinnerMode="dropdown" | ||
android:layout_marginTop="4dp" | ||
android:layout_margin="2dp" /> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<LinearLayout | ||
android:layout_centerHorizontal="true" | ||
android:layout_width="wrap_content" | ||
android:orientation="horizontal" | ||
android:layout_height="wrap_content"> | ||
|
||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/cancel" | ||
android:id="@+id/cancel_button" | ||
android:layout_margin="2dp" /> | ||
|
||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/select_button" | ||
android:id="@+id/select_button" | ||
android:layout_margin="2dp" /> | ||
|
||
</LinearLayout> | ||
|
||
</RelativeLayout> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters