1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.accountswitcher;
20
21 import org.exoplatform.R;
22 import org.exoplatform.accountswitcher.AccountSwitcherProxy.AccountSwitcherListener;
23 import org.exoplatform.model.ExoAccount;
24 import org.exoplatform.singleton.AccountSetting;
25 import org.exoplatform.singleton.ServerSettingHelper;
26
27 import android.os.Bundle;
28 import android.support.v4.app.Fragment;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import android.widget.AdapterView.OnItemClickListener;
35 import android.widget.ListView;
36
37
38
39
40
41 public class AccountSwitcherFragment extends Fragment implements AccountSwitcherListener {
42
43
44
45
46 private ListView mAccountListView;
47
48
49
50
51 private OnItemClickListener mOnItemClickListener;
52
53 public static final String FRAGMENT_TAG = "account_switcher_fragment_dialog";
54
55 public static final String TAG = "eXo____AccountSwitcherFragment____";
56
57 public AccountSwitcherFragment() {
58 mOnItemClickListener = new OnItemClickListener() {
59 @Override
60 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
61 if (AccountSetting.getInstance().getDomainIndex().equals(String.valueOf(position))) {
62
63 dismissFragment();
64 } else {
65
66
67 ExoAccount account = ServerSettingHelper.getInstance().getServerInfoList(getActivity()).get(position);
68 if (account != null)
69 switchToAccount(account);
70 }
71 }
72 };
73 }
74
75 @Override
76 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77
78 View layout = inflater.inflate(R.layout.account_switcher_fragment, container, false);
79
80 mAccountListView = (ListView) layout.findViewById(R.id.account_list_view);
81 AccountListAdapter accountsAdapter = new AccountListAdapter(getActivity());
82 mAccountListView.setAdapter(accountsAdapter);
83 mAccountListView.setOnItemClickListener(mOnItemClickListener);
84
85 return layout;
86 }
87
88
89
90
91
92 void dismissFragment() {
93 getActivity().finish();
94 }
95
96
97
98
99
100
101
102
103 private void switchToAccount(ExoAccount account) {
104 AccountSwitcherProxy controller = new AccountSwitcherProxy(getActivity(), this, false);
105 controller.switchToAccount(account);
106 }
107
108
109
110
111
112
113
114 private void openSignInFragment(ExoAccount account) {
115
116 Log.i(TAG, "Open the sign in form to get username and password of the account " + account.accountName);
117
118 SignInFragment signInFragment = new SignInFragment(account);
119
120
121 getActivity().getSupportFragmentManager()
122 .beginTransaction()
123 .setCustomAnimations(R.anim.fragment_enter_rtl, R.anim.fragment_exit_rtl)
124 .replace(R.id.share_extension_fragment, signInFragment, SignInFragment.FRAGMENT_TAG)
125 .addToBackStack(FRAGMENT_TAG)
126 .commit();
127 }
128
129 @Override
130 public void onSwitchAccountFinished(boolean result) {
131
132 AccountSwitcherActivity parentActivity = (AccountSwitcherActivity) getActivity();
133 if (result)
134
135 parentActivity.redirectToHomeScreenAndFinish();
136 else
137
138 parentActivity.redirectToLoginScreenAndFinish();
139 }
140
141 @Override
142 public void onMissingPassword(ExoAccount account) {
143
144 openSignInFragment(account);
145 }
146
147 @Override
148 public void onAccountInvalid(ExoAccount account) {
149
150
151
152
153
154 }
155
156 }