Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.settings.accounts;
     17 
     18 import android.accounts.Account;
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.os.UserHandle;
     23 import android.os.UserManager;
     24 import android.support.annotation.VisibleForTesting;
     25 import android.support.v7.preference.PreferenceScreen;
     26 
     27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     28 import com.android.settings.R;
     29 import com.android.settings.Utils;
     30 import com.android.settings.dashboard.DashboardFragment;
     31 import com.android.settingslib.accounts.AuthenticatorHelper;
     32 import com.android.settingslib.core.AbstractPreferenceController;
     33 import com.android.settingslib.drawer.Tile;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 public class AccountDetailDashboardFragment extends DashboardFragment {
     39 
     40     private static final String TAG = "AccountDetailDashboard";
     41     private static final String METADATA_IA_ACCOUNT = "com.android.settings.ia.account";
     42     private static final String EXTRA_ACCOUNT_NAME = "extra.accountName";
     43 
     44     public static final String KEY_ACCOUNT = "account";
     45     public static final String KEY_ACCOUNT_TYPE = "account_type";
     46     public static final String KEY_ACCOUNT_LABEL = "account_label";
     47     public static final String KEY_ACCOUNT_TITLE_RES = "account_title_res";
     48     public static final String KEY_USER_HANDLE = "user_handle";
     49 
     50     @VisibleForTesting
     51     Account mAccount;
     52     private String mAccountLabel;
     53     @VisibleForTesting
     54     String mAccountType;
     55     private AccountSyncPreferenceController mAccountSynController;
     56     private RemoveAccountPreferenceController mRemoveAccountController;
     57 
     58     @Override
     59     public void onCreate(Bundle icicle) {
     60         super.onCreate(icicle);
     61         getPreferenceManager().setPreferenceComparisonCallback(null);
     62         Bundle args = getArguments();
     63         final Activity activity = getActivity();
     64         UserHandle userHandle = Utils.getSecureTargetUser(activity.getActivityToken(),
     65                 (UserManager) getSystemService(Context.USER_SERVICE), args,
     66                 activity.getIntent().getExtras());
     67         if (args != null) {
     68             if (args.containsKey(KEY_ACCOUNT)) {
     69                 mAccount = args.getParcelable(KEY_ACCOUNT);
     70             }
     71             if (args.containsKey(KEY_ACCOUNT_LABEL)) {
     72                 mAccountLabel = args.getString(KEY_ACCOUNT_LABEL);
     73             }
     74             if (args.containsKey(KEY_ACCOUNT_TYPE)) {
     75                 mAccountType = args.getString(KEY_ACCOUNT_TYPE);
     76             }
     77         }
     78         mAccountSynController.init(mAccount, userHandle);
     79         mRemoveAccountController.init(mAccount, userHandle);
     80     }
     81 
     82     @Override
     83     public void onActivityCreated(Bundle savedInstanceState) {
     84         super.onActivityCreated(savedInstanceState);
     85         if (mAccountLabel != null) {
     86             getActivity().setTitle(mAccountLabel);
     87         }
     88         updateUi();
     89     }
     90 
     91     @Override
     92     public int getMetricsCategory() {
     93         return MetricsEvent.ACCOUNT;
     94     }
     95 
     96     @Override
     97     protected String getLogTag() {
     98         return TAG;
     99     }
    100 
    101     @Override
    102     public int getHelpResource() {
    103         return R.string.help_url_account_detail;
    104     }
    105 
    106     @Override
    107     protected int getPreferenceScreenResId() {
    108         return R.xml.account_type_settings;
    109     }
    110 
    111     @Override
    112     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
    113         final List<AbstractPreferenceController> controllers = new ArrayList<>();
    114         mAccountSynController = new AccountSyncPreferenceController(context);
    115         controllers.add(mAccountSynController);
    116         mRemoveAccountController = new RemoveAccountPreferenceController(context, this);
    117         controllers.add(mRemoveAccountController);
    118         controllers.add(new AccountHeaderPreferenceController(
    119                 context, getLifecycle(), getActivity(), this /* host */, getArguments()));
    120         return controllers;
    121     }
    122 
    123     @Override
    124     protected boolean displayTile(Tile tile) {
    125         if (mAccountType == null) {
    126             return false;
    127         }
    128         final Bundle metadata = tile.metaData;
    129         if (metadata == null) {
    130             return false;
    131         }
    132         final boolean display = mAccountType.equals(metadata.getString(METADATA_IA_ACCOUNT));
    133         if (display && tile.intent != null) {
    134             tile.intent.putExtra(EXTRA_ACCOUNT_NAME, mAccount.name);
    135         }
    136         return display;
    137     }
    138 
    139     @VisibleForTesting
    140     void updateUi() {
    141         final Context context = getContext();
    142         UserHandle userHandle = null;
    143         Bundle args = getArguments();
    144         if (args != null && args.containsKey(KEY_USER_HANDLE)) {
    145             userHandle = args.getParcelable(KEY_USER_HANDLE);
    146         }
    147         final AuthenticatorHelper helper = new AuthenticatorHelper(context, userHandle, null);
    148         final AccountTypePreferenceLoader accountTypePreferenceLoader =
    149                 new AccountTypePreferenceLoader(this, helper, userHandle);
    150         PreferenceScreen prefs = accountTypePreferenceLoader.addPreferencesForType(
    151                 mAccountType, getPreferenceScreen());
    152         if (prefs != null) {
    153             accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount);
    154         }
    155     }
    156 }