blob: 68ead09d9d57414ed646d98c2176ff30f8a20157 [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 Monkfcad09a2016-06-07 14:01:32 -040030import android.util.ArraySet;
Jason Monke79790b2015-12-02 15:39:19 -050031import android.util.Log;
Jason Monk744b6362015-11-03 18:24:29 -050032import android.view.LayoutInflater;
Jason Monk744b6362015-11-03 18:24:29 -050033import android.view.View;
34import android.view.ViewGroup;
35import android.view.Window;
Jason Monkcafda1f2016-02-05 10:16:22 -050036import android.view.WindowManager.LayoutParams;
Fan Zhang7163d772016-03-29 10:30:13 -070037import android.widget.FrameLayout;
Jason Monk744b6362015-11-03 18:24:29 -050038import android.widget.Toolbar;
Fan Zhang7163d772016-03-29 10:30:13 -070039
Jason Monk744b6362015-11-03 18:24:29 -050040import com.android.settingslib.R;
41
Jason Monke79790b2015-12-02 15:39:19 -050042import java.util.ArrayList;
Jason Monk744b6362015-11-03 18:24:29 -050043import java.util.List;
44
45public class SettingsDrawerActivity extends Activity {
46
Jason Monke79790b2015-12-02 15:39:19 -050047 protected static final boolean DEBUG_TIMING = false;
48 private static final String TAG = "SettingsDrawerActivity";
Fan Zhang089a2c02016-09-15 14:28:19 -070049 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monke79790b2015-12-02 15:39:19 -050050
Clara Bayarri962694d2016-04-29 16:56:02 +010051 public static final String EXTRA_SHOW_MENU = "show_drawer_menu";
Jason Monk946483a2016-01-22 15:19:44 -050052
Jason Monkfcad09a2016-06-07 14:01:32 -040053 // Serves as a temporary list of tiles to ignore until we heard back from the PM that they
54 // are disabled.
55 private static ArraySet<ComponentName> sTileBlacklist = new ArraySet<>();
Jason Monke79790b2015-12-02 15:39:19 -050056
57 private final PackageReceiver mPackageReceiver = new PackageReceiver();
58 private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
59
Fan Zhang7163d772016-03-29 10:30:13 -070060 private FrameLayout mContentHeaderContainer;
Jason Monk744b6362015-11-03 18:24:29 -050061
62 @Override
63 protected void onCreate(@Nullable Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65
Jason Monke79790b2015-12-02 15:39:19 -050066 long startTime = System.currentTimeMillis();
67
Udam Saini48987f62016-03-03 13:20:18 -080068 TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme);
69 if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
70 getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Udam Saini48987f62016-03-03 13:20:18 -080071 requestWindowFeature(Window.FEATURE_NO_TITLE);
72 }
Jason Monk744b6362015-11-03 18:24:29 -050073 super.setContentView(R.layout.settings_with_drawer);
Fan Zhang92a26132018-02-06 14:24:18 -080074 mContentHeaderContainer = findViewById(R.id.content_header_container);
Fan Zhangf1c1bb42017-02-27 16:04:32 -080075
Fan Zhang92a26132018-02-06 14:24:18 -080076 Toolbar toolbar = findViewById(R.id.action_bar);
Jason Monkf18a55d2015-11-12 10:52:21 -050077 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
78 toolbar.setVisibility(View.GONE);
Jason Monkf18a55d2015-11-12 10:52:21 -050079 return;
80 }
Jason Monk744b6362015-11-03 18:24:29 -050081 setActionBar(toolbar);
jackqdyuleidfc6ce52016-08-24 09:48:54 -070082
Fan Zhangf1c1bb42017-02-27 16:04:32 -080083 if (DEBUG_TIMING) {
84 Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
85 + " ms");
Jason Monk744b6362015-11-03 18:24:29 -050086 }
Jason Monk744b6362015-11-03 18:24:29 -050087 }
88
89 @Override
Fan Zhangcf439c02017-01-03 13:06:11 -080090 public boolean onNavigateUp() {
Fan Zhang92a26132018-02-06 14:24:18 -080091 if (!super.onNavigateUp()) {
92 finish();
93 }
Fan Zhangf1c1bb42017-02-27 16:04:32 -080094 return true;
Fan Zhangcf439c02017-01-03 13:06:11 -080095 }
96
97 @Override
Jason Monk744b6362015-11-03 18:24:29 -050098 protected void onResume() {
99 super.onResume();
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800100 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
101 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
102 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
103 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
104 filter.addDataScheme("package");
105 registerReceiver(mPackageReceiver, filter);
Jason Monk744b6362015-11-03 18:24:29 -0500106
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800107 new CategoriesUpdateTask().execute();
Jason Monke79790b2015-12-02 15:39:19 -0500108 }
109
110 @Override
111 protected void onPause() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800112 unregisterReceiver(mPackageReceiver);
Jason Monke79790b2015-12-02 15:39:19 -0500113 super.onPause();
114 }
115
116 public void addCategoryListener(CategoryListener listener) {
117 mCategoryListeners.add(listener);
118 }
119
120 public void remCategoryListener(CategoryListener listener) {
121 mCategoryListeners.remove(listener);
Jason Monk744b6362015-11-03 18:24:29 -0500122 }
123
Jason Monk744b6362015-11-03 18:24:29 -0500124 @Override
125 public void setContentView(@LayoutRes int layoutResID) {
Fan Zhang34106dd2017-04-21 10:06:21 -0700126 final ViewGroup parent = findViewById(R.id.content_frame);
Jason Monk4ea66572016-03-30 11:05:51 -0400127 if (parent != null) {
128 parent.removeAllViews();
129 }
130 LayoutInflater.from(this).inflate(layoutResID, parent);
Jason Monk744b6362015-11-03 18:24:29 -0500131 }
132
133 @Override
134 public void setContentView(View view) {
135 ((ViewGroup) findViewById(R.id.content_frame)).addView(view);
136 }
137
138 @Override
139 public void setContentView(View view, ViewGroup.LayoutParams params) {
140 ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params);
141 }
142
Fan Zhang34106dd2017-04-21 10:06:21 -0700143 private void onCategoriesChanged() {
Jason Monke79790b2015-12-02 15:39:19 -0500144 final int N = mCategoryListeners.size();
145 for (int i = 0; i < N; i++) {
146 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500147 }
Jason Monk744b6362015-11-03 18:24:29 -0500148 }
149
Fan Zhangf5e032b2017-07-05 15:24:03 -0700150 /**
151 * @return whether or not the enabled state actually changed.
152 */
153 public boolean setTileEnabled(ComponentName component, boolean enabled) {
Jason Monkfcad09a2016-06-07 14:01:32 -0400154 PackageManager pm = getPackageManager();
155 int state = pm.getComponentEnabledSetting(component);
156 boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
157 if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
158 if (enabled) {
159 sTileBlacklist.remove(component);
160 } else {
161 sTileBlacklist.add(component);
162 }
163 pm.setComponentEnabledSetting(component, enabled
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800164 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
165 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
Jason Monkfcad09a2016-06-07 14:01:32 -0400166 PackageManager.DONT_KILL_APP);
Fan Zhangf5e032b2017-07-05 15:24:03 -0700167 return true;
Fan Zhangd2d3e2cc2017-01-06 11:44:38 -0800168 }
Fan Zhangf5e032b2017-07-05 15:24:03 -0700169 return false;
Fan Zhangd2d3e2cc2017-01-06 11:44:38 -0800170 }
171
172 /**
173 * Updates dashboard categories. Only necessary to call this after setTileEnabled
174 */
175 public void updateCategories() {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800176 new CategoriesUpdateTask().execute();
Jason Monkfcad09a2016-06-07 14:01:32 -0400177 }
178
roger xue8f06ab02016-12-08 14:09:50 -0800179 public String getSettingPkg() {
180 return TileUtils.SETTING_PKG;
181 }
182
Jason Monke79790b2015-12-02 15:39:19 -0500183 public interface CategoryListener {
184 void onCategoriesChanged();
185 }
186
Fan Zhang22a56d72016-09-27 17:52:00 -0700187 private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> {
188
189 private final CategoryManager mCategoryManager;
190
191 public CategoriesUpdateTask() {
Fan Zhang5adc2662016-10-04 15:12:58 -0700192 mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this);
Fan Zhang22a56d72016-09-27 17:52:00 -0700193 }
194
195 @Override
196 protected Void doInBackground(Void... params) {
roger xue8f06ab02016-12-08 14:09:50 -0800197 mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg());
Fan Zhang22a56d72016-09-27 17:52:00 -0700198 return null;
199 }
200
201 @Override
202 protected void onPostExecute(Void result) {
203 mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist);
204 onCategoriesChanged();
205 }
206 }
207
Jason Monke79790b2015-12-02 15:39:19 -0500208 private class PackageReceiver extends BroadcastReceiver {
209 @Override
210 public void onReceive(Context context, Intent intent) {
Fan Zhangf1c1bb42017-02-27 16:04:32 -0800211 new CategoriesUpdateTask().execute();
Jason Monke79790b2015-12-02 15:39:19 -0500212 }
213 }
Jason Monk744b6362015-11-03 18:24:29 -0500214}