View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * Created by The eXo Platform SAS Author : Philippe Aristote
39   * paristote@exoplatform.com Sep 3, 2014
40   */
41  public class AccountSwitcherFragment extends Fragment implements AccountSwitcherListener {
42  
43    /**
44     * ListView that contains account items
45     */
46    private ListView            mAccountListView;
47  
48    /**
49     * Listener that is called when an account item is tapped
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            // If the current account was selected, return to the Home screen
63            dismissFragment();
64          } else {
65            // Otherwise, get the account at the given position and start the
66            // switching operation
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     * Returns to the Home activity when the currently connected account is
90     * selected by the user.
91     */
92    void dismissFragment() {
93      getActivity().finish();
94    }
95  
96    /**
97     * Launches the switching procedure.<br/>
98     * If the controller notifies that the password is missing, open the dialog to
99     * ask the user to type it.
100    * 
101    * @param account
102    */
103   private void switchToAccount(ExoAccount account) {
104     AccountSwitcherProxy controller = new AccountSwitcherProxy(getActivity(), this, false);
105     controller.switchToAccount(account);
106   }
107 
108   /**
109    * Open the SignIn fragment to let the user type his password and sign in
110    * again.
111    * 
112    * @param account
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     // TODO handle animation on returning from signin fragment to this fragment
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     // TODO surround with try..catch in case of ClassCastExc
132     AccountSwitcherActivity parentActivity = (AccountSwitcherActivity) getActivity();
133     if (result)
134       // Login successful
135       parentActivity.redirectToHomeScreenAndFinish();
136     else
137       // Login failed
138       parentActivity.redirectToLoginScreenAndFinish();
139   }
140 
141   @Override
142   public void onMissingPassword(ExoAccount account) {
143     // password is unknown, we must ask the user via the SignIn fragment
144     openSignInFragment(account);
145   }
146 
147   @Override
148   public void onAccountInvalid(ExoAccount account) {
149     /*
150      * TODO possible actions: - open the account edition activity for this
151      * account to enter a URL - show a message to the user - close the account
152      * switcher
153      */
154   }
155 
156 }