blob: 457ce76d79ac00c923b43075ba21561bee5737d7 [file] [log] [blame]
Jason Monk744b6362015-11-03 18:24:29 -05001/**
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 Zhangf1c1bb42017-02-27 16:04:32 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Jason Monk744b6362015-11-03 18:24:29 -05009 *
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 */
16package com.android.settingslib.drawer;
17
18import android.annotation.LayoutRes;
19import android.annotation.Nullable;
20import android.app.Activity;
Jason Monke79790b2015-12-02 15:39:19 -050021import android.content.BroadcastReceiver;
Jason Monkfcad09a2016-06-07 14:01:32 -040022import android.content.ComponentName;
Jason Monke79790b2015-12-02 15:39:19 -050023import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Jason Monkfcad09a2016-06-07 14:01:32 -040026import android.content.pm.PackageManager;
Jason Monkf18a55d2015-11-12 10:52:21 -050027import android.content.res.TypedArray;
Jason Monke79790b2015-12-02 15:39:19 -050028import android.os.AsyncTask;
Jason Monk744b6362015-11-03 18:24:29 -050029import android.os.Bundle;
Jason Monkcafda1f2016-02-05 10:16:22 -050030import android.provider.Settings;
Jason Monkfcad09a2016-06-07 14:01:32 -040031import android.util.ArraySet;
Jason Monke79790b2015-12-02 15:39:19 -050032import android.util.Log;
Jason Monk744b6362015-11-03 18:24:29 -050033import android.util.Pair;
Jason Monk744b6362015-11-03 18:24:29 -050034import android.view.LayoutInflater;
Jason Monk744b6362015-11-03 18:24:29 -050035import android.view.View;
36import android.view.ViewGroup;
37import android.view.Window;
Jason Monkcafda1f2016-02-05 10:16:22 -050038import android.view.WindowManager.LayoutParams;
Fan Zhang7163d772016-03-29 10:30:13 -070039import android.widget.FrameLayout;
Jason Monk744b6362015-11-03 18:24:29 -050040import android.widget.Toolbar;
Fan Zhang7163d772016-03-29 10:30:13 -070041
Jason Monk744b6362015-11-03 18:24:29 -050042import com.android.settingslib.R;
Jason Monkec02e472016-03-28 10:44:43 -040043import com.android.settingslib.applications.InterestingConfigChanges;
Jason Monk744b6362015-11-03 18:24:29 -050044
Jason Monke79790b2015-12-02 15:39:19 -050045import java.util.ArrayList;
Jason Monk744b6362015-11-03 18:24:29 -050046import java.util.HashMap;
47import java.util.List;
48
49public class SettingsDrawerActivity extends Activity {
50
Jason Monke79790b2015-12-02 15:39:19 -050051 protected static final boolean DEBUG_TIMING = false;
52 private static final String TAG = "SettingsDrawerActivity";
Fan Zhang089a2c02016-09-15 14:28:19 -070053 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monke79790b2015-12-02 15:39:19 -050054
Clara Bayarri962694d2016-04-29 16:56:02 +010055 public static final String EXTRA_SHOW_MENU = "show_drawer_menu";
Jason Monk946483a2016-01-22 15:19:44 -050056
Jason Monkfcad09a2016-06-07 14:01:32 -040057 // Serves as a temporary list of tiles to ignore until we heard back from the PM that they
58 // are disabled.
59 private static ArraySet<ComponentName> sTileBlacklist = new ArraySet<>();
Jason Monke79790b2015-12-02 15:39:19 -050060
61 private final PackageReceiver mPackageReceiver = new PackageReceiver();
62 private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
63
Fan Zhang7163d772016-03-29 10:30:13 -070064 private FrameLayout mContentHeaderContainer;
Jason Monk744b6362015-11-03 18:24:29 -050065
Fan Zhang22a56d72016-09-27 17:52:00 -070066 // Remove below after new IA
67 @Deprecated
68 private static List<DashboardCategory> sDashboardCategories;
69 @Deprecated
70 private static HashMap<Pair<String, String>, Tile> sTileCache;
71 @Deprecated
72 private static InterestingConfigChanges sConfigTracker;
73 // Remove above after new IA
74
Jason Monk744b6362015-11-03 18:24:29 -050075 @Override
76 protected void onCreate(@Nullable Bundle savedInstanceState) {
77 super.onCreate(savedInstanceState);
78
Jason Monke79790b2015-12-02 15:39:19 -050079 long startTime = System.currentTimeMillis();
80
Udam Saini48987f62016-03-03 13:20:18 -080081 TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme);
82 if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
83 getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Udam Saini48987f62016-03-03 13:20:18 -080084 requestWindowFeature(Window.FEATURE_NO_TITLE);
85 }
Jason Monk744b6362015-11-03 18:24:29 -050086 super.setContentView(R.layout.settings_with_drawer);
Fan Zhang7163d772016-03-29 10:30:13 -070087 mContentHeaderContainer = (FrameLayout) findViewById(R.id.content_header_container);
Fan Zhangf1c1bb42017-02-27 16:04:32 -080088
Jason Monk744b6362015-11-03 18:24:29 -050089 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
Jason Monkf18a55d2015-11-12 10:52:21 -050090 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
91 toolbar.setVisibility(View.GONE);
Jason Monkf18a55d2015-11-12 10:52:21 -050092 return;
93 }
Jason Monk744b6362015-11-03 18:24:29 -050094 setActionBar(toolbar);
jackqdyuleidfc6ce52016-08-24 09:48:54 -070095
Fan Zhangf1c1bb42017-02-27 16:04:32 -080096 if (DEBUG_TIMING) {
97 Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
98 + " ms");
Jason Monk744b6362015-11-03 18:24:29 -050099 }
Jason Monk744b6362015-11-03 18:24:29 -0500100 }
101
102 @Override
Fan Zhangcf439c02017-01-03 13:06:11 -0800103 public boolean onNavigateUp() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800104 finish();
105 return true;
Fan Zhangcf439c02017-01-03 13:06:11 -0800106 }
107
108 @Override
Jason Monk744b6362015-11-03 18:24:29 -0500109 protected void onResume() {
110 super.onResume();
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800111 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
112 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
113 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
114 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
115 filter.addDataScheme("package");
116 registerReceiver(mPackageReceiver, filter);
Jason Monk744b6362015-11-03 18:24:29 -0500117
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800118 new CategoriesUpdateTask().execute();
Fan Zhangf311b742016-09-16 08:46:09 -0700119 final Intent intent = getIntent();
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800120 if (intent != null && intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) {
121 // Intent explicitly set to show menu.
122 showMenuIcon();
Jason Monk946483a2016-01-22 15:19:44 -0500123 }
Jason Monke79790b2015-12-02 15:39:19 -0500124 }
125
126 @Override
127 protected void onPause() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800128 unregisterReceiver(mPackageReceiver);
Jason Monke79790b2015-12-02 15:39:19 -0500129 super.onPause();
130 }
131
roger xue8f06ab02016-12-08 14:09:50 -0800132 /**
133 * Gets the name of the intent action of the default setting app. Used to launch setting app
134 * when Settings Home is clicked.
135 */
136 public String getSettingAction() {
137 return Settings.ACTION_SETTINGS;
138 }
139
Jason Monke79790b2015-12-02 15:39:19 -0500140 public void addCategoryListener(CategoryListener listener) {
141 mCategoryListeners.add(listener);
142 }
143
144 public void remCategoryListener(CategoryListener listener) {
145 mCategoryListeners.remove(listener);
Jason Monk744b6362015-11-03 18:24:29 -0500146 }
147
Fan Zhang7163d772016-03-29 10:30:13 -0700148 public void setContentHeaderView(View headerView) {
149 mContentHeaderContainer.removeAllViews();
150 if (headerView != null) {
151 mContentHeaderContainer.addView(headerView);
152 }
153 }
154
Jason Monk744b6362015-11-03 18:24:29 -0500155 @Override
156 public void setContentView(@LayoutRes int layoutResID) {
Jason Monk4ea66572016-03-30 11:05:51 -0400157 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
158 if (parent != null) {
159 parent.removeAllViews();
160 }
161 LayoutInflater.from(this).inflate(layoutResID, parent);
Jason Monk744b6362015-11-03 18:24:29 -0500162 }
163
164 @Override
165 public void setContentView(View view) {
166 ((ViewGroup) findViewById(R.id.content_frame)).addView(view);
167 }
168
169 @Override
170 public void setContentView(View view, ViewGroup.LayoutParams params) {
171 ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params);
172 }
173
Jason Monk946483a2016-01-22 15:19:44 -0500174 public void showMenuIcon() {
Jason Monk946483a2016-01-22 15:19:44 -0500175 getActionBar().setDisplayHomeAsUpEnabled(true);
176 }
177
Jason Monke79790b2015-12-02 15:39:19 -0500178 public List<DashboardCategory> getDashboardCategories() {
Jason Monk2a5d79a2015-12-03 10:51:58 -0500179 if (sDashboardCategories == null) {
180 sTileCache = new HashMap<>();
Jason Monkec02e472016-03-28 10:44:43 -0400181 sConfigTracker = new InterestingConfigChanges();
Jason Monkb0ae8f82016-04-12 10:30:43 -0400182 // Apply initial current config.
183 sConfigTracker.applyNewConfig(getResources());
Jason Monk2a5d79a2015-12-03 10:51:58 -0500184 sDashboardCategories = TileUtils.getCategories(this, sTileCache);
185 }
Jason Monke79790b2015-12-02 15:39:19 -0500186 return sDashboardCategories;
187 }
188
189 protected void onCategoriesChanged() {
Jason Monke79790b2015-12-02 15:39:19 -0500190 final int N = mCategoryListeners.size();
191 for (int i = 0; i < N; i++) {
192 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500193 }
Jason Monk744b6362015-11-03 18:24:29 -0500194 }
195
Jason Monk0d72d202015-11-04 13:16:00 -0500196 public void onProfileTileOpen() {
Jason Monk744b6362015-11-03 18:24:29 -0500197 finish();
198 }
Jason Monke79790b2015-12-02 15:39:19 -0500199
Jason Monkfcad09a2016-06-07 14:01:32 -0400200 public void setTileEnabled(ComponentName component, boolean enabled) {
201 PackageManager pm = getPackageManager();
202 int state = pm.getComponentEnabledSetting(component);
203 boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
204 if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
205 if (enabled) {
206 sTileBlacklist.remove(component);
207 } else {
208 sTileBlacklist.add(component);
209 }
210 pm.setComponentEnabledSetting(component, enabled
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800211 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
212 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
Jason Monkfcad09a2016-06-07 14:01:32 -0400213 PackageManager.DONT_KILL_APP);
Fan Zhangd2d3e2cc2017-01-06 11:44:38 -0800214 }
215 }
216
217 /**
218 * Updates dashboard categories. Only necessary to call this after setTileEnabled
219 */
220 public void updateCategories() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800221 new CategoriesUpdateTask().execute();
Jason Monkfcad09a2016-06-07 14:01:32 -0400222 }
223
roger xue8f06ab02016-12-08 14:09:50 -0800224 public String getSettingPkg() {
225 return TileUtils.SETTING_PKG;
226 }
227
Jason Monke79790b2015-12-02 15:39:19 -0500228 public interface CategoryListener {
229 void onCategoriesChanged();
230 }
231
Fan Zhang22a56d72016-09-27 17:52:00 -0700232 private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> {
233
234 private final CategoryManager mCategoryManager;
235
236 public CategoriesUpdateTask() {
Fan Zhang5adc2662016-10-04 15:12:58 -0700237 mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this);
Fan Zhang22a56d72016-09-27 17:52:00 -0700238 }
239
240 @Override
241 protected Void doInBackground(Void... params) {
roger xue8f06ab02016-12-08 14:09:50 -0800242 mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg());
Fan Zhang22a56d72016-09-27 17:52:00 -0700243 return null;
244 }
245
246 @Override
247 protected void onPostExecute(Void result) {
248 mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist);
249 onCategoriesChanged();
250 }
251 }
252
Jason Monke79790b2015-12-02 15:39:19 -0500253 private class PackageReceiver extends BroadcastReceiver {
254 @Override
255 public void onReceive(Context context, Intent intent) {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800256 new CategoriesUpdateTask().execute();
Jason Monke79790b2015-12-02 15:39:19 -0500257 }
258 }
Jason Monk744b6362015-11-03 18:24:29 -0500259}