Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2015 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 | * |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 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.settingslib.drawer; |
| 17 | |
| 18 | import android.annotation.LayoutRes; |
| 19 | import android.annotation.Nullable; |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame^] | 20 | import android.app.ActionBar; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 21 | import android.app.Activity; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 23 | import android.content.ComponentName; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 29 | import android.os.AsyncTask; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 30 | import android.os.Bundle; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 31 | import android.util.ArraySet; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 32 | import android.util.Log; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.view.Window; |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 37 | import android.view.WindowManager.LayoutParams; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 38 | import android.widget.FrameLayout; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 39 | import android.widget.Toolbar; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 40 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 41 | import com.android.settingslib.R; |
| 42 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 43 | import java.util.ArrayList; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 44 | import java.util.List; |
| 45 | |
| 46 | public class SettingsDrawerActivity extends Activity { |
| 47 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 48 | protected static final boolean DEBUG_TIMING = false; |
| 49 | private static final String TAG = "SettingsDrawerActivity"; |
Fan Zhang | 089a2c0 | 2016-09-15 14:28:19 -0700 | [diff] [blame] | 50 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 51 | |
Clara Bayarri | 962694d | 2016-04-29 16:56:02 +0100 | [diff] [blame] | 52 | public static final String EXTRA_SHOW_MENU = "show_drawer_menu"; |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 53 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 54 | // Serves as a temporary list of tiles to ignore until we heard back from the PM that they |
| 55 | // are disabled. |
| 56 | private static ArraySet<ComponentName> sTileBlacklist = new ArraySet<>(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 57 | |
| 58 | private final PackageReceiver mPackageReceiver = new PackageReceiver(); |
| 59 | private final List<CategoryListener> mCategoryListeners = new ArrayList<>(); |
| 60 | |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 61 | private FrameLayout mContentHeaderContainer; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 65 | super.onCreate(savedInstanceState); |
| 66 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 67 | long startTime = System.currentTimeMillis(); |
| 68 | |
Udam Saini | 48987f6 | 2016-03-03 13:20:18 -0800 | [diff] [blame] | 69 | TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme); |
| 70 | if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 71 | getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
Udam Saini | 48987f6 | 2016-03-03 13:20:18 -0800 | [diff] [blame] | 72 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 73 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 74 | super.setContentView(R.layout.settings_with_drawer); |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 75 | mContentHeaderContainer = (FrameLayout) findViewById(R.id.content_header_container); |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 76 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 77 | Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 78 | if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 79 | toolbar.setVisibility(View.GONE); |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 80 | return; |
| 81 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 82 | setActionBar(toolbar); |
jackqdyulei | dfc6ce5 | 2016-08-24 09:48:54 -0700 | [diff] [blame] | 83 | |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 84 | if (DEBUG_TIMING) { |
| 85 | Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime) |
| 86 | + " ms"); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 87 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 91 | public boolean onNavigateUp() { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 92 | finish(); |
| 93 | return true; |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 97 | protected void onResume() { |
| 98 | super.onResume(); |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 99 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 100 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 101 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 102 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 103 | filter.addDataScheme("package"); |
| 104 | registerReceiver(mPackageReceiver, filter); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 105 | |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 106 | new CategoriesUpdateTask().execute(); |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 107 | final Intent intent = getIntent(); |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 108 | if (intent != null && intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) { |
| 109 | // Intent explicitly set to show menu. |
| 110 | showMenuIcon(); |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 111 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onPause() { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 116 | unregisterReceiver(mPackageReceiver); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 117 | super.onPause(); |
| 118 | } |
| 119 | |
| 120 | public void addCategoryListener(CategoryListener listener) { |
| 121 | mCategoryListeners.add(listener); |
| 122 | } |
| 123 | |
| 124 | public void remCategoryListener(CategoryListener listener) { |
| 125 | mCategoryListeners.remove(listener); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 128 | public void setContentHeaderView(View headerView) { |
| 129 | mContentHeaderContainer.removeAllViews(); |
| 130 | if (headerView != null) { |
| 131 | mContentHeaderContainer.addView(headerView); |
| 132 | } |
| 133 | } |
| 134 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 135 | @Override |
| 136 | public void setContentView(@LayoutRes int layoutResID) { |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame^] | 137 | final ViewGroup parent = findViewById(R.id.content_frame); |
Jason Monk | 4ea6657 | 2016-03-30 11:05:51 -0400 | [diff] [blame] | 138 | if (parent != null) { |
| 139 | parent.removeAllViews(); |
| 140 | } |
| 141 | LayoutInflater.from(this).inflate(layoutResID, parent); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void setContentView(View view) { |
| 146 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 151 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 152 | } |
| 153 | |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame^] | 154 | private void showMenuIcon() { |
| 155 | final ActionBar actionBar = getActionBar(); |
| 156 | if (actionBar != null) { |
| 157 | actionBar.setDisplayHomeAsUpEnabled(true); |
| 158 | } |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame^] | 161 | private void onCategoriesChanged() { |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 162 | final int N = mCategoryListeners.size(); |
| 163 | for (int i = 0; i < N; i++) { |
| 164 | mCategoryListeners.get(i).onCategoriesChanged(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 165 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 168 | public void onProfileTileOpen() { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 169 | finish(); |
| 170 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 171 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 172 | public void setTileEnabled(ComponentName component, boolean enabled) { |
| 173 | PackageManager pm = getPackageManager(); |
| 174 | int state = pm.getComponentEnabledSetting(component); |
| 175 | boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 176 | if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 177 | if (enabled) { |
| 178 | sTileBlacklist.remove(component); |
| 179 | } else { |
| 180 | sTileBlacklist.add(component); |
| 181 | } |
| 182 | pm.setComponentEnabledSetting(component, enabled |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 183 | ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 184 | : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 185 | PackageManager.DONT_KILL_APP); |
Fan Zhang | d2d3e2cc | 2017-01-06 11:44:38 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Updates dashboard categories. Only necessary to call this after setTileEnabled |
| 191 | */ |
| 192 | public void updateCategories() { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 193 | new CategoriesUpdateTask().execute(); |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 194 | } |
| 195 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 196 | public String getSettingPkg() { |
| 197 | return TileUtils.SETTING_PKG; |
| 198 | } |
| 199 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 200 | public interface CategoryListener { |
| 201 | void onCategoriesChanged(); |
| 202 | } |
| 203 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 204 | private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> { |
| 205 | |
| 206 | private final CategoryManager mCategoryManager; |
| 207 | |
| 208 | public CategoriesUpdateTask() { |
Fan Zhang | 5adc266 | 2016-10-04 15:12:58 -0700 | [diff] [blame] | 209 | mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected Void doInBackground(Void... params) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 214 | mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg()); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 215 | return null; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | protected void onPostExecute(Void result) { |
| 220 | mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist); |
| 221 | onCategoriesChanged(); |
| 222 | } |
| 223 | } |
| 224 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 225 | private class PackageReceiver extends BroadcastReceiver { |
| 226 | @Override |
| 227 | public void onReceive(Context context, Intent intent) { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 228 | new CategoriesUpdateTask().execute(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 231 | } |