• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.car.settings.accounts;
17 
18 import android.accounts.Account;
19 import android.accounts.AccountManager;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.annotation.XmlRes;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.SettingsFragment;
31 import com.android.car.ui.toolbar.ToolbarController;
32 import com.android.settingslib.accounts.AuthenticatorHelper;
33 
34 import java.util.Arrays;
35 
36 /**
37  * Shows account details, and delete account option.
38  */
39 public class AccountDetailsFragment extends SettingsFragment implements
40         AuthenticatorHelper.OnAccountsUpdateListener {
41     public static final String EXTRA_ACCOUNT = "extra_account";
42     public static final String EXTRA_ACCOUNT_LABEL = "extra_account_label";
43     public static final String EXTRA_USER_INFO = "extra_user_info";
44 
45     private Account mAccount;
46     private UserInfo mUserInfo;
47     private AuthenticatorHelper mAuthenticatorHelper;
48 
49     /**
50      * Creates a new AccountDetailsFragment.
51      *
52      * <p>Passes the provided account, label, and user info to the fragment via fragment arguments.
53      */
newInstance(Account account, CharSequence label, UserInfo userInfo)54     public static AccountDetailsFragment newInstance(Account account, CharSequence label,
55             UserInfo userInfo) {
56         AccountDetailsFragment
57                 accountDetailsFragment = new AccountDetailsFragment();
58         Bundle bundle = new Bundle();
59         bundle.putParcelable(EXTRA_ACCOUNT, account);
60         bundle.putCharSequence(EXTRA_ACCOUNT_LABEL, label);
61         bundle.putParcelable(EXTRA_USER_INFO, userInfo);
62         accountDetailsFragment.setArguments(bundle);
63         return accountDetailsFragment;
64     }
65 
66     @Override
67     @XmlRes
getPreferenceScreenResId()68     protected int getPreferenceScreenResId() {
69         return R.xml.account_details_fragment;
70     }
71 
72     @Override
onAttach(Context context)73     public void onAttach(Context context) {
74         super.onAttach(context);
75 
76         mAccount = getArguments().getParcelable(EXTRA_ACCOUNT);
77         mUserInfo = getArguments().getParcelable(EXTRA_USER_INFO);
78 
79         use(AccountDetailsPreferenceController.class, R.string.pk_account_details)
80                 .setAccount(mAccount);
81         use(AccountDetailsPreferenceController.class, R.string.pk_account_details)
82                 .setUserHandle(mUserInfo.getUserHandle());
83 
84         use(AccountSyncPreferenceController.class, R.string.pk_account_sync)
85                 .setAccount(mAccount);
86         use(AccountDetailsSettingController.class, R.string.pk_account_settings)
87                 .setAccount(mAccount);
88         use(AccountSyncPreferenceController.class, R.string.pk_account_sync)
89                 .setUserHandle(mUserInfo.getUserHandle());
90     }
91 
92     @Override
setupToolbar(@onNull ToolbarController toolbar)93     protected void setupToolbar(@NonNull ToolbarController toolbar) {
94         super.setupToolbar(toolbar);
95 
96         // Set the fragment's title
97         toolbar.setTitle(getArguments().getCharSequence(EXTRA_ACCOUNT_LABEL));
98     }
99 
100     @Override
onStart()101     public void onStart() {
102         super.onStart();
103 
104         mAuthenticatorHelper = new AuthenticatorHelper(getContext(), mUserInfo.getUserHandle(),
105                 this);
106         mAuthenticatorHelper.listenToAccountUpdates();
107     }
108 
109     @Override
onStop()110     public void onStop() {
111         super.onStop();
112         mAuthenticatorHelper.stopListeningToAccountUpdates();
113     }
114 
115     @Override
onAccountsUpdate(UserHandle userHandle)116     public void onAccountsUpdate(UserHandle userHandle) {
117         if (!accountExists()) {
118             // The account was deleted. Pop back.
119             goBack();
120         }
121     }
122 
123     /** Returns whether the account being shown by this fragment still exists. */
124     @VisibleForTesting
accountExists()125     boolean accountExists() {
126         if (mAccount == null) {
127             return false;
128         }
129 
130         Account[] accounts = AccountManager.get(getContext()).getAccountsByTypeAsUser(mAccount.type,
131                 mUserInfo.getUserHandle());
132 
133         return Arrays.asList(accounts).contains(mAccount);
134     }
135 }
136