1 /* 2 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.settings.accounts; 19 20 import android.app.Activity; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.SyncStatusObserver; 24 import android.graphics.drawable.Drawable; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.text.format.DateFormat; 29 import android.util.Log; 30 31 import com.android.settings.SettingsPreferenceFragment; 32 import com.android.settings.Utils; 33 import com.android.settingslib.accounts.AuthenticatorHelper; 34 import com.android.settingslib.utils.ThreadUtils; 35 36 import java.util.Date; 37 38 abstract class AccountPreferenceBase extends SettingsPreferenceFragment 39 implements AuthenticatorHelper.OnAccountsUpdateListener { 40 41 protected static final String TAG = "AccountPreferenceBase"; 42 protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 43 44 public static final String AUTHORITIES_FILTER_KEY = "authorities"; 45 public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types"; 46 47 private UserManager mUm; 48 private Object mStatusChangeListenerHandle; 49 protected AuthenticatorHelper mAuthenticatorHelper; 50 protected UserHandle mUserHandle; 51 protected AccountTypePreferenceLoader mAccountTypePreferenceLoader; 52 53 private java.text.DateFormat mDateFormat; 54 private java.text.DateFormat mTimeFormat; 55 56 @Override 57 public void onCreate(Bundle icicle) { 58 super.onCreate(icicle); 59 mUm = (UserManager) getSystemService(Context.USER_SERVICE); 60 final Activity activity = getActivity(); 61 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(), 62 activity.getIntent().getExtras()); 63 mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this); 64 mAccountTypePreferenceLoader = 65 new AccountTypePreferenceLoader(this, mAuthenticatorHelper, mUserHandle); 66 } 67 68 /** 69 * Overload to handle account updates. 70 */ 71 @Override 72 public void onAccountsUpdate(UserHandle userHandle) { 73 74 } 75 76 /** 77 * Overload to handle authenticator description updates 78 */ 79 protected void onAuthDescriptionsUpdated() { 80 81 } 82 83 /** 84 * Overload to handle sync state updates. 85 */ 86 protected void onSyncStateUpdated() { 87 88 } 89 90 @Override 91 public void onActivityCreated(Bundle savedInstanceState) { 92 super.onActivityCreated(savedInstanceState); 93 94 final Activity activity = getActivity(); 95 96 mDateFormat = DateFormat.getDateFormat(activity); 97 mTimeFormat = DateFormat.getTimeFormat(activity); 98 } 99 100 @Override 101 public void onResume() { 102 super.onResume(); 103 mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener( 104 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE 105 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS 106 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, 107 mSyncStatusObserver); 108 onSyncStateUpdated(); 109 } 110 111 @Override 112 public void onPause() { 113 super.onPause(); 114 ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle); 115 } 116 117 private SyncStatusObserver mSyncStatusObserver = 118 which -> ThreadUtils.postOnMainThread(() -> onSyncStateUpdated()); 119 120 public void updateAuthDescriptions() { 121 mAuthenticatorHelper.updateAuthDescriptions(getActivity()); 122 onAuthDescriptionsUpdated(); 123 } 124 125 protected Drawable getDrawableForType(final String accountType) { 126 return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType); 127 } 128 129 protected CharSequence getLabelForType(final String accountType) { 130 return mAuthenticatorHelper.getLabelForType(getActivity(), accountType); 131 } 132 133 protected String formatSyncDate(Date date) { 134 // TODO: Switch to using DateUtils.formatDateTime 135 return mDateFormat.format(date) + " " + mTimeFormat.format(date); 136 } 137 } 138