Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
| 17 | package com.android.systemui.qs.customize; |
| 18 | |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 19 | import android.Manifest.permission; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 20 | import android.app.ActivityManager; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.os.AsyncTask; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.service.quicksettings.TileService; |
Jason Monk | 50a352b | 2016-06-13 12:43:47 -0400 | [diff] [blame] | 31 | import android.widget.Button; |
| 32 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 33 | import com.android.systemui.R; |
| 34 | import com.android.systemui.qs.QSTile; |
| 35 | import com.android.systemui.qs.QSTile.DrawableIcon; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 36 | import com.android.systemui.qs.QSTile.State; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 37 | import com.android.systemui.qs.external.CustomTile; |
| 38 | import com.android.systemui.statusbar.phone.QSTileHost; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Collection; |
| 42 | import java.util.List; |
| 43 | |
| 44 | public class TileQueryHelper { |
| 45 | |
| 46 | private static final String TAG = "TileQueryHelper"; |
| 47 | |
| 48 | private final ArrayList<TileInfo> mTiles = new ArrayList<>(); |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 49 | private final ArrayList<String> mSpecs = new ArrayList<>(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 50 | private final Context mContext; |
| 51 | private TileStateListener mListener; |
| 52 | |
| 53 | public TileQueryHelper(Context context, QSTileHost host) { |
| 54 | mContext = context; |
| 55 | addSystemTiles(host); |
| 56 | // TODO: Live? |
| 57 | } |
| 58 | |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 59 | private void addSystemTiles(final QSTileHost host) { |
Will Harmon | beb1f17 | 2016-06-06 14:52:40 -0700 | [diff] [blame] | 60 | String possible = mContext.getString(R.string.quick_settings_tiles_stock); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 61 | String[] possibleTiles = possible.split(","); |
| 62 | final Handler qsHandler = new Handler(host.getLooper()); |
| 63 | final Handler mainHandler = new Handler(Looper.getMainLooper()); |
| 64 | for (int i = 0; i < possibleTiles.length; i++) { |
| 65 | final String spec = possibleTiles[i]; |
| 66 | final QSTile<?> tile = host.createTile(spec); |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 67 | if (tile == null) { |
| 68 | continue; |
| 69 | } else if (!tile.isAvailable()) { |
| 70 | tile.destroy(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 71 | continue; |
| 72 | } |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 73 | tile.setListening(this, true); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 74 | tile.clearState(); |
| 75 | tile.refreshState(); |
Jason Monk | e5107a3 | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 76 | tile.setListening(this, false); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 77 | qsHandler.post(new Runnable() { |
| 78 | @Override |
| 79 | public void run() { |
| 80 | final QSTile.State state = tile.newTileState(); |
| 81 | tile.getState().copyTo(state); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 82 | // Ignore the current state and get the generic label instead. |
| 83 | state.label = tile.getTileLabel(); |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 84 | tile.destroy(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 85 | mainHandler.post(new Runnable() { |
| 86 | @Override |
| 87 | public void run() { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 88 | addTile(spec, null, state, true); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 89 | mListener.onTilesChanged(mTiles); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | qsHandler.post(new Runnable() { |
| 96 | @Override |
| 97 | public void run() { |
Jason Monk | cb654cb | 2016-02-25 15:43:07 -0500 | [diff] [blame] | 98 | mainHandler.post(new Runnable() { |
| 99 | @Override |
| 100 | public void run() { |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 101 | new QueryTilesTask().execute(host.getTiles()); |
Jason Monk | cb654cb | 2016-02-25 15:43:07 -0500 | [diff] [blame] | 102 | } |
| 103 | }); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 104 | } |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | public void setListener(TileStateListener listener) { |
| 109 | mListener = listener; |
| 110 | } |
| 111 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 112 | private void addTile(String spec, CharSequence appLabel, State state, boolean isSystem) { |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 113 | if (mSpecs.contains(spec)) { |
| 114 | return; |
| 115 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 116 | TileInfo info = new TileInfo(); |
| 117 | info.state = state; |
Jason Monk | 50a352b | 2016-06-13 12:43:47 -0400 | [diff] [blame] | 118 | info.state.minimalAccessibilityClassName = info.state.expandedAccessibilityClassName = |
| 119 | Button.class.getName(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 120 | info.spec = spec; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 121 | info.appLabel = appLabel; |
| 122 | info.isSystem = isSystem; |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 123 | mTiles.add(info); |
| 124 | mSpecs.add(spec); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 127 | private void addTile(String spec, Drawable drawable, CharSequence label, CharSequence appLabel, |
| 128 | Context context) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 129 | QSTile.State state = new QSTile.State(); |
| 130 | state.label = label; |
| 131 | state.contentDescription = label; |
| 132 | state.icon = new DrawableIcon(drawable); |
Yoshinori Hirano | eb09362 | 2016-08-17 14:09:58 +0900 | [diff] [blame] | 133 | state.autoMirrorDrawable = false; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 134 | addTile(spec, appLabel, state, false); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public static class TileInfo { |
| 138 | public String spec; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 139 | public CharSequence appLabel; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 140 | public QSTile.State state; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 141 | public boolean isSystem; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 144 | private class QueryTilesTask extends |
| 145 | AsyncTask<Collection<QSTile<?>>, Void, Collection<TileInfo>> { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 146 | @Override |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 147 | protected Collection<TileInfo> doInBackground(Collection<QSTile<?>>... params) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 148 | List<TileInfo> tiles = new ArrayList<>(); |
| 149 | PackageManager pm = mContext.getPackageManager(); |
| 150 | List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 151 | new Intent(TileService.ACTION_QS_TILE), 0, ActivityManager.getCurrentUser()); |
Will Harmon | beb1f17 | 2016-06-06 14:52:40 -0700 | [diff] [blame] | 152 | String stockTiles = mContext.getString(R.string.quick_settings_tiles_stock); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 153 | for (ResolveInfo info : services) { |
| 154 | String packageName = info.serviceInfo.packageName; |
| 155 | ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name); |
Will Harmon | beb1f17 | 2016-06-06 14:52:40 -0700 | [diff] [blame] | 156 | |
| 157 | // Don't include apps that are a part of the default tile set. |
| 158 | if (stockTiles.contains(componentName.flattenToString())) { |
| 159 | continue; |
| 160 | } |
| 161 | |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 162 | final CharSequence appLabel = info.serviceInfo.applicationInfo.loadLabel(pm); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 163 | String spec = CustomTile.toSpec(componentName); |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 164 | State state = getState(params[0], spec); |
| 165 | if (state != null) { |
| 166 | addTile(spec, appLabel, state, false); |
| 167 | continue; |
| 168 | } |
Jason Monk | a5f6ed3 | 2016-06-22 09:58:46 -0400 | [diff] [blame] | 169 | if (info.serviceInfo.icon == 0 && info.serviceInfo.applicationInfo.icon == 0) { |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 170 | continue; |
| 171 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 172 | Drawable icon = info.serviceInfo.loadIcon(pm); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 173 | if (!permission.BIND_QUICK_SETTINGS_TILE.equals(info.serviceInfo.permission)) { |
| 174 | continue; |
| 175 | } |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 176 | if (icon == null) { |
| 177 | continue; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 178 | } |
Jason Monk | 4a906f9 | 2016-04-20 10:54:55 -0400 | [diff] [blame] | 179 | icon.mutate(); |
| 180 | icon.setTint(mContext.getColor(android.R.color.white)); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 181 | CharSequence label = info.serviceInfo.loadLabel(pm); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 182 | addTile(spec, icon, label != null ? label.toString() : "null", appLabel, mContext); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 183 | } |
| 184 | return tiles; |
| 185 | } |
| 186 | |
Jason Monk | 21582d4 | 2016-04-21 16:14:27 -0400 | [diff] [blame] | 187 | private State getState(Collection<QSTile<?>> tiles, String spec) { |
| 188 | for (QSTile<?> tile : tiles) { |
| 189 | if (spec.equals(tile.getTileSpec())) { |
| 190 | final QSTile.State state = tile.newTileState(); |
| 191 | tile.getState().copyTo(state); |
| 192 | return state; |
| 193 | } |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 198 | @Override |
| 199 | protected void onPostExecute(Collection<TileInfo> result) { |
| 200 | mTiles.addAll(result); |
| 201 | mListener.onTilesChanged(mTiles); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public interface TileStateListener { |
| 206 | void onTilesChanged(List<TileInfo> tiles); |
| 207 | } |
| 208 | } |