blob: 190f5e6d140255084343ed0a9272c797fc6307f9 [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;
Fan Zhang34106dd2017-04-21 10:06:21 -070020import android.app.ActionBar;
Jason Monk744b6362015-11-03 18:24:29 -050021import android.app.Activity;
Jason Monke79790b2015-12-02 15:39:19 -050022import android.content.BroadcastReceiver;
Jason Monkfcad09a2016-06-07 14:01:32 -040023import android.content.ComponentName;
Jason Monke79790b2015-12-02 15:39:19 -050024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Jason Monkfcad09a2016-06-07 14:01:32 -040027import android.content.pm.PackageManager;
Jason Monkf18a55d2015-11-12 10:52:21 -050028import android.content.res.TypedArray;
Jason Monke79790b2015-12-02 15:39:19 -050029import android.os.AsyncTask;
Jason Monk744b6362015-11-03 18:24:29 -050030import android.os.Bundle;
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.view.LayoutInflater;
Jason Monk744b6362015-11-03 18:24:29 -050034import android.view.View;
35import android.view.ViewGroup;
36import android.view.Window;
Jason Monkcafda1f2016-02-05 10:16:22 -050037import android.view.WindowManager.LayoutParams;
Fan Zhang7163d772016-03-29 10:30:13 -070038import android.widget.FrameLayout;
Jason Monk744b6362015-11-03 18:24:29 -050039import android.widget.Toolbar;
Fan Zhang7163d772016-03-29 10:30:13 -070040
Jason Monk744b6362015-11-03 18:24:29 -050041import com.android.settingslib.R;
42
Jason Monke79790b2015-12-02 15:39:19 -050043import java.util.ArrayList;
Jason Monk744b6362015-11-03 18:24:29 -050044import java.util.List;
45
46public class SettingsDrawerActivity extends Activity {
47
Jason Monke79790b2015-12-02 15:39:19 -050048 protected static final boolean DEBUG_TIMING = false;
49 private static final String TAG = "SettingsDrawerActivity";
Fan Zhang089a2c02016-09-15 14:28:19 -070050 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monke79790b2015-12-02 15:39:19 -050051
Clara Bayarri962694d2016-04-29 16:56:02 +010052 public static final String EXTRA_SHOW_MENU = "show_drawer_menu";
Jason Monk946483a2016-01-22 15:19:44 -050053
Jason Monkfcad09a2016-06-07 14:01:32 -040054 // 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 Monke79790b2015-12-02 15:39:19 -050057
58 private final PackageReceiver mPackageReceiver = new PackageReceiver();
59 private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
60
Fan Zhang7163d772016-03-29 10:30:13 -070061 private FrameLayout mContentHeaderContainer;
Jason Monk744b6362015-11-03 18:24:29 -050062
63 @Override
64 protected void onCreate(@Nullable Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66
Jason Monke79790b2015-12-02 15:39:19 -050067 long startTime = System.currentTimeMillis();
68
Udam Saini48987f62016-03-03 13:20:18 -080069 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 Saini48987f62016-03-03 13:20:18 -080072 requestWindowFeature(Window.FEATURE_NO_TITLE);
73 }
Jason Monk744b6362015-11-03 18:24:29 -050074 super.setContentView(R.layout.settings_with_drawer);
Fan Zhang7163d772016-03-29 10:30:13 -070075 mContentHeaderContainer = (FrameLayout) findViewById(R.id.content_header_container);
Fan Zhangf1c1bb42017-02-27 16:04:32 -080076
Jason Monk744b6362015-11-03 18:24:29 -050077 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
Jason Monkf18a55d2015-11-12 10:52:21 -050078 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
79 toolbar.setVisibility(View.GONE);
Jason Monkf18a55d2015-11-12 10:52:21 -050080 return;
81 }
Jason Monk744b6362015-11-03 18:24:29 -050082 setActionBar(toolbar);
jackqdyuleidfc6ce52016-08-24 09:48:54 -070083
Fan Zhangf1c1bb42017-02-27 16:04:32 -080084 if (DEBUG_TIMING) {
85 Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
86 + " ms");
Jason Monk744b6362015-11-03 18:24:29 -050087 }
Jason Monk744b6362015-11-03 18:24:29 -050088 }
89
90 @Override
Fan Zhangcf439c02017-01-03 13:06:11 -080091 public boolean onNavigateUp() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -080092 finish();
93 return true;
Fan Zhangcf439c02017-01-03 13:06:11 -080094 }
95
96 @Override
Jason Monk744b6362015-11-03 18:24:29 -050097 protected void onResume() {
98 super.onResume();
Fan Zhangf1c1bb42017-02-27 16:04:32 -080099 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 Monk744b6362015-11-03 18:24:29 -0500105
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800106 new CategoriesUpdateTask().execute();
Fan Zhangf311b742016-09-16 08:46:09 -0700107 final Intent intent = getIntent();
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800108 if (intent != null && intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) {
109 // Intent explicitly set to show menu.
110 showMenuIcon();
Jason Monk946483a2016-01-22 15:19:44 -0500111 }
Jason Monke79790b2015-12-02 15:39:19 -0500112 }
113
114 @Override
115 protected void onPause() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800116 unregisterReceiver(mPackageReceiver);
Jason Monke79790b2015-12-02 15:39:19 -0500117 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 Monk744b6362015-11-03 18:24:29 -0500126 }
127
Fan Zhang7163d772016-03-29 10:30:13 -0700128 public void setContentHeaderView(View headerView) {
129 mContentHeaderContainer.removeAllViews();
130 if (headerView != null) {
131 mContentHeaderContainer.addView(headerView);
132 }
133 }
134
Jason Monk744b6362015-11-03 18:24:29 -0500135 @Override
136 public void setContentView(@LayoutRes int layoutResID) {
Fan Zhang34106dd2017-04-21 10:06:21 -0700137 final ViewGroup parent = findViewById(R.id.content_frame);
Jason Monk4ea66572016-03-30 11:05:51 -0400138 if (parent != null) {
139 parent.removeAllViews();
140 }
141 LayoutInflater.from(this).inflate(layoutResID, parent);
Jason Monk744b6362015-11-03 18:24:29 -0500142 }
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 Zhang34106dd2017-04-21 10:06:21 -0700154 private void showMenuIcon() {
155 final ActionBar actionBar = getActionBar();
156 if (actionBar != null) {
157 actionBar.setDisplayHomeAsUpEnabled(true);
158 }
Jason Monk946483a2016-01-22 15:19:44 -0500159 }
160
Fan Zhang34106dd2017-04-21 10:06:21 -0700161 private void onCategoriesChanged() {
Jason Monke79790b2015-12-02 15:39:19 -0500162 final int N = mCategoryListeners.size();
163 for (int i = 0; i < N; i++) {
164 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500165 }
Jason Monk744b6362015-11-03 18:24:29 -0500166 }
167
Jason Monk0d72d202015-11-04 13:16:00 -0500168 public void onProfileTileOpen() {
Jason Monk744b6362015-11-03 18:24:29 -0500169 finish();
170 }
Jason Monke79790b2015-12-02 15:39:19 -0500171
Fan Zhangf5e032b2017-07-05 15:24:03 -0700172 /**
173 * @return whether or not the enabled state actually changed.
174 */
175 public boolean setTileEnabled(ComponentName component, boolean enabled) {
Jason Monkfcad09a2016-06-07 14:01:32 -0400176 PackageManager pm = getPackageManager();
177 int state = pm.getComponentEnabledSetting(component);
178 boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
179 if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
180 if (enabled) {
181 sTileBlacklist.remove(component);
182 } else {
183 sTileBlacklist.add(component);
184 }
185 pm.setComponentEnabledSetting(component, enabled
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800186 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
187 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
Jason Monkfcad09a2016-06-07 14:01:32 -0400188 PackageManager.DONT_KILL_APP);
Fan Zhangf5e032b2017-07-05 15:24:03 -0700189 return true;
Fan Zhangd2d3e2cc2017-01-06 11:44:38 -0800190 }
Fan Zhangf5e032b2017-07-05 15:24:03 -0700191 return false;
Fan Zhangd2d3e2cc2017-01-06 11:44:38 -0800192 }
193
194 /**
195 * Updates dashboard categories. Only necessary to call this after setTileEnabled
196 */
197 public void updateCategories() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800198 new CategoriesUpdateTask().execute();
Jason Monkfcad09a2016-06-07 14:01:32 -0400199 }
200
roger xue8f06ab02016-12-08 14:09:50 -0800201 public String getSettingPkg() {
202 return TileUtils.SETTING_PKG;
203 }
204
Jason Monke79790b2015-12-02 15:39:19 -0500205 public interface CategoryListener {
206 void onCategoriesChanged();
207 }
208
Fan Zhang22a56d72016-09-27 17:52:00 -0700209 private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> {
210
211 private final CategoryManager mCategoryManager;
212
213 public CategoriesUpdateTask() {
Fan Zhang5adc2662016-10-04 15:12:58 -0700214 mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this);
Fan Zhang22a56d72016-09-27 17:52:00 -0700215 }
216
217 @Override
218 protected Void doInBackground(Void... params) {
roger xue8f06ab02016-12-08 14:09:50 -0800219 mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg());
Fan Zhang22a56d72016-09-27 17:52:00 -0700220 return null;
221 }
222
223 @Override
224 protected void onPostExecute(Void result) {
225 mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist);
226 onCategoriesChanged();
227 }
228 }
229
Jason Monke79790b2015-12-02 15:39:19 -0500230 private class PackageReceiver extends BroadcastReceiver {
231 @Override
232 public void onReceive(Context context, Intent intent) {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800233 new CategoriesUpdateTask().execute();
Jason Monke79790b2015-12-02 15:39:19 -0500234 }
235 }
Jason Monk744b6362015-11-03 18:24:29 -0500236}