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; |
| 34 | import com.android.systemui.qs.external.CustomTile; |
| 35 | import com.android.systemui.statusbar.phone.QSTileHost; |
| 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collection; |
| 39 | import java.util.List; |
| 40 | |
| 41 | public class TileQueryHelper { |
| 42 | |
| 43 | private static final String TAG = "TileQueryHelper"; |
| 44 | |
| 45 | private final ArrayList<TileInfo> mTiles = new ArrayList<>(); |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 46 | private final ArrayList<String> mSpecs = new ArrayList<>(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 47 | private final Context mContext; |
| 48 | private TileStateListener mListener; |
| 49 | |
| 50 | public TileQueryHelper(Context context, QSTileHost host) { |
| 51 | mContext = context; |
| 52 | addSystemTiles(host); |
| 53 | // TODO: Live? |
| 54 | } |
| 55 | |
| 56 | private void addSystemTiles(QSTileHost host) { |
Jason Monk | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 57 | boolean hasColorMod = host.getNightModeController().isEnabled(); |
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 | 5dbd4aa | 2016-02-07 13:13:39 -0500 | [diff] [blame] | 59 | + ",hotspot,inversion,saver,work,cast" + (hasColorMod ? ",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); |
| 78 | mainHandler.post(new Runnable() { |
| 79 | @Override |
| 80 | public void run() { |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 81 | addTile(spec, state); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 82 | mListener.onTilesChanged(mTiles); |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | }); |
| 87 | } |
| 88 | qsHandler.post(new Runnable() { |
| 89 | @Override |
| 90 | public void run() { |
| 91 | new QueryTilesTask().execute(); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | public void setListener(TileStateListener listener) { |
| 97 | mListener = listener; |
| 98 | } |
| 99 | |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 100 | private void addTile(String spec, QSTile.State state) { |
| 101 | if (mSpecs.contains(spec)) { |
| 102 | return; |
| 103 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 104 | TileInfo info = new TileInfo(); |
| 105 | info.state = state; |
| 106 | info.spec = spec; |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 107 | mTiles.add(info); |
| 108 | mSpecs.add(spec); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 111 | private void addTile(String spec, Drawable drawable, CharSequence label, Context context) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 112 | QSTile.State state = new QSTile.State(); |
| 113 | state.label = label; |
| 114 | state.contentDescription = label; |
| 115 | state.icon = new DrawableIcon(drawable); |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 116 | addTile(spec, state); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public static class TileInfo { |
| 120 | public String spec; |
| 121 | public QSTile.State state; |
| 122 | } |
| 123 | |
| 124 | private class QueryTilesTask extends AsyncTask<Void, Void, Collection<TileInfo>> { |
| 125 | @Override |
| 126 | protected Collection<TileInfo> doInBackground(Void... params) { |
| 127 | List<TileInfo> tiles = new ArrayList<>(); |
| 128 | PackageManager pm = mContext.getPackageManager(); |
| 129 | List<ResolveInfo> services = pm.queryIntentServicesAsUser( |
| 130 | new Intent(TileService.ACTION_QS_TILE), 0, ActivityManager.getCurrentUser()); |
| 131 | for (ResolveInfo info : services) { |
| 132 | String packageName = info.serviceInfo.packageName; |
| 133 | ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name); |
| 134 | String spec = CustomTile.toSpec(componentName); |
| 135 | Drawable icon = info.serviceInfo.loadIcon(pm); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 136 | if (!permission.BIND_QUICK_SETTINGS_TILE.equals(info.serviceInfo.permission)) { |
| 137 | continue; |
| 138 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 139 | if (icon != null) { |
| 140 | icon.mutate(); |
| 141 | icon.setTint(mContext.getColor(android.R.color.white)); |
| 142 | } |
| 143 | CharSequence label = info.serviceInfo.loadLabel(pm); |
Jason Monk | e24194f | 2016-02-05 14:55:05 -0500 | [diff] [blame] | 144 | addTile(spec, icon, label != null ? label.toString() : "null", mContext); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 145 | } |
| 146 | return tiles; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected void onPostExecute(Collection<TileInfo> result) { |
| 151 | mTiles.addAll(result); |
| 152 | mListener.onTilesChanged(mTiles); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public interface TileStateListener { |
| 157 | void onTilesChanged(List<TileInfo> tiles); |
| 158 | } |
| 159 | } |