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; |
| 31 | import com.android.systemui.R; |
| 32 | import com.android.systemui.qs.QSTile; |
| 33 | import com.android.systemui.qs.QSTile.DrawableIcon; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 34 | import com.android.systemui.qs.QSTile.State; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 35 | import com.android.systemui.qs.external.CustomTile; |
| 36 | import com.android.systemui.statusbar.phone.QSTileHost; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Collection; |
| 40 | import java.util.List; |
| 41 | |
| 42 | public class TileQueryHelper { |
| 43 | |
| 44 | private static final String TAG = "TileQueryHelper"; |
| 45 | |
| 46 | private final ArrayList<TileInfo> mTiles = new ArrayList<>(); |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 47 | private final ArrayList<String> mSpecs = new ArrayList<>(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 48 | private final Context mContext; |
| 49 | private TileStateListener mListener; |
| 50 | |
| 51 | public TileQueryHelper(Context context, QSTileHost host) { |
| 52 | mContext = context; |
| 53 | addSystemTiles(host); |
| 54 | // TODO: Live? |
| 55 | } |
| 56 | |
| 57 | private void addSystemTiles(QSTileHost host) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 58 | String possible = mContext.getString(R.string.quick_settings_tiles_default) |
Jason Monk | 1b0afeb | 2016-03-03 18:25:54 -0500 | [diff] [blame] | 59 | + ",hotspot,inversion,saver,work,cast,night"; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 60 | String[] possibleTiles = possible.split(","); |
| 61 | final Handler qsHandler = new Handler(host.getLooper()); |
| 62 | final Handler mainHandler = new Handler(Looper.getMainLooper()); |
| 63 | for (int i = 0; i < possibleTiles.length; i++) { |
| 64 | final String spec = possibleTiles[i]; |
| 65 | final QSTile<?> tile = host.createTile(spec); |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 66 | if (tile == null || !tile.isAvailable()) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 67 | continue; |
| 68 | } |
| 69 | tile.setListening(true); |
| 70 | tile.clearState(); |
| 71 | tile.refreshState(); |
| 72 | tile.setListening(false); |
| 73 | qsHandler.post(new Runnable() { |
| 74 | @Override |
| 75 | public void run() { |
| 76 | final QSTile.State state = tile.newTileState(); |
| 77 | tile.getState().copyTo(state); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 78 | // Ignore the current state and get the generic label instead. |
| 79 | state.label = tile.getTileLabel(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 80 | mainHandler.post(new Runnable() { |
| 81 | @Override |
| 82 | public void run() { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 83 | addTile(spec, null, state, true); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 84 | mListener.onTilesChanged(mTiles); |
| 85 | } |
| 86 | }); |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | qsHandler.post(new Runnable() { |
| 91 | @Override |
| 92 | public void run() { |
Jason Monk | cb654cb | 2016-02-25 15:43:07 -0500 | [diff] [blame] | 93 | mainHandler.post(new Runnable() { |
| 94 | @Override |
| 95 | public void run() { |
| 96 | new QueryTilesTask().execute(); |
| 97 | } |
| 98 | }); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | public void setListener(TileStateListener listener) { |
| 104 | mListener = listener; |
| 105 | } |
| 106 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 107 | private void addTile(String spec, CharSequence appLabel, State state, boolean isSystem) { |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 108 | if (mSpecs.contains(spec)) { |
| 109 | return; |
| 110 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 111 | TileInfo info = new TileInfo(); |
| 112 | info.state = state; |
| 113 | info.spec = spec; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 114 | info.appLabel = appLabel; |
| 115 | info.isSystem = isSystem; |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 116 | mTiles.add(info); |
| 117 | mSpecs.add(spec); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 120 | private void addTile(String spec, Drawable drawable, CharSequence label, CharSequence appLabel, |
| 121 | Context context) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 122 | QSTile.State state = new QSTile.State(); |
| 123 | state.label = label; |
| 124 | state.contentDescription = label; |
| 125 | state.icon = new DrawableIcon(drawable); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 126 | addTile(spec, appLabel, state, false); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public static class TileInfo { |
| 130 | public String spec; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 131 | public CharSequence appLabel; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 132 | public QSTile.State state; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 133 | public boolean isSystem; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private class QueryTilesTask extends AsyncTask<Void, Void, Collection<TileInfo>> { |
| 137 | @Override |
| 138 | protected Collection<TileInfo> doInBackground(Void... params) { |
| 139 | List<TileInfo> tiles = new ArrayList<>(); |
| 140 | PackageManager pm = mContext.getPackageManager(); |
| 141 | List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 142 | new Intent(TileService.ACTION_QS_TILE), 0, ActivityManager.getCurrentUser()); |
| 143 | for (ResolveInfo info : services) { |
| 144 | String packageName = info.serviceInfo.packageName; |
| 145 | ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name); |
| 146 | String spec = CustomTile.toSpec(componentName); |
| 147 | Drawable icon = info.serviceInfo.loadIcon(pm); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 148 | if (!permission.BIND_QUICK_SETTINGS_TILE.equals(info.serviceInfo.permission)) { |
| 149 | continue; |
| 150 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 151 | if (icon != null) { |
| 152 | icon.mutate(); |
| 153 | icon.setTint(mContext.getColor(android.R.color.white)); |
| 154 | } |
| 155 | CharSequence label = info.serviceInfo.loadLabel(pm); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 156 | final CharSequence appLabel = info.serviceInfo.applicationInfo.loadLabel(pm); |
| 157 | addTile(spec, icon, label != null ? label.toString() : "null", appLabel, mContext); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 158 | } |
| 159 | return tiles; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void onPostExecute(Collection<TileInfo> result) { |
| 164 | mTiles.addAll(result); |
| 165 | mListener.onTilesChanged(mTiles); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public interface TileStateListener { |
| 170 | void onTilesChanged(List<TileInfo> tiles); |
| 171 | } |
| 172 | } |