Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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.example.androidx.slice.demos; |
| 18 | |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 19 | import static com.example.androidx.slice.demos.SampleSliceProvider.URI_PATHS; |
| 20 | import static com.example.androidx.slice.demos.SampleSliceProvider.getUri; |
| 21 | |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 22 | import android.arch.lifecycle.LiveData; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 24 | import android.content.Intent; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 25 | import android.content.pm.ActivityInfo; |
Mady Mellor | c6620e7 | 2017-11-28 13:29:57 -0800 | [diff] [blame] | 26 | import android.content.pm.ApplicationInfo; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.database.Cursor; |
| 30 | import android.database.MatrixCursor; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
| 33 | import android.provider.BaseColumns; |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 34 | import android.support.annotation.NonNull; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 35 | import android.support.annotation.RequiresApi; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 36 | import android.support.v7.app.AppCompatActivity; |
| 37 | import android.support.v7.widget.Toolbar; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 38 | import android.util.ArrayMap; |
| 39 | import android.util.Log; |
| 40 | import android.view.Menu; |
| 41 | import android.view.MenuItem; |
| 42 | import android.view.SubMenu; |
| 43 | import android.view.ViewGroup; |
| 44 | import android.widget.CursorAdapter; |
| 45 | import android.widget.SearchView; |
| 46 | import android.widget.SimpleCursorAdapter; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 47 | |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.Comparator; |
| 50 | import java.util.List; |
| 51 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 52 | import androidx.app.slice.Slice; |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 53 | import androidx.app.slice.SliceItem; |
| 54 | import androidx.app.slice.widget.EventInfo; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 55 | import androidx.app.slice.widget.SliceLiveData; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 56 | import androidx.app.slice.widget.SliceView; |
| 57 | |
| 58 | /** |
| 59 | * Example use of SliceView. Uses a search bar to select/auto-complete a slice uri which is |
| 60 | * then displayed in the selected mode with SliceView. |
| 61 | */ |
| 62 | @RequiresApi(api = 28) |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 63 | public class SliceBrowser extends AppCompatActivity implements SliceView.SliceObserver { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 64 | |
| 65 | private static final String TAG = "SlicePresenter"; |
| 66 | |
| 67 | private static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 68 | private static final boolean TEST_INTENT = false; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 69 | |
| 70 | private ArrayList<Uri> mSliceUris = new ArrayList<Uri>(); |
| 71 | private int mSelectedMode; |
| 72 | private ViewGroup mContainer; |
| 73 | private SearchView mSearchView; |
| 74 | private SimpleCursorAdapter mAdapter; |
| 75 | private SubMenu mTypeMenu; |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 76 | private LiveData<Slice> mSliceLiveData; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public void onCreate(Bundle savedInstanceState) { |
| 80 | super.onCreate(savedInstanceState); |
| 81 | setContentView(R.layout.activity_layout); |
| 82 | |
| 83 | Toolbar toolbar = findViewById(R.id.search_toolbar); |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 84 | setSupportActionBar(toolbar); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 85 | |
| 86 | // Shows the slice |
| 87 | mContainer = findViewById(R.id.slice_preview); |
| 88 | mSearchView = findViewById(R.id.search_view); |
| 89 | |
| 90 | final String[] from = new String[]{"uri"}; |
| 91 | final int[] to = new int[]{android.R.id.text1}; |
| 92 | mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, |
| 93 | null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); |
| 94 | mSearchView.setSuggestionsAdapter(mAdapter); |
| 95 | mSearchView.setIconifiedByDefault(false); |
| 96 | mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { |
| 97 | @Override |
| 98 | public boolean onSuggestionClick(int position) { |
| 99 | mSearchView.setQuery(((Cursor) mAdapter.getItem(position)).getString(1), true); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean onSuggestionSelect(int position) { |
| 105 | mSearchView.setQuery(((Cursor) mAdapter.getItem(position)).getString(1), true); |
| 106 | return true; |
| 107 | } |
| 108 | }); |
| 109 | mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { |
| 110 | @Override |
| 111 | public boolean onQueryTextSubmit(String s) { |
| 112 | addSlice(Uri.parse(s)); |
| 113 | mSearchView.clearFocus(); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean onQueryTextChange(String s) { |
| 119 | populateAdapter(s); |
| 120 | return false; |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | mSelectedMode = (savedInstanceState != null) |
| 125 | ? savedInstanceState.getInt("SELECTED_MODE", SliceView.MODE_SHORTCUT) |
| 126 | : SliceView.MODE_SHORTCUT; |
| 127 | if (savedInstanceState != null) { |
| 128 | mSearchView.setQuery(savedInstanceState.getString("SELECTED_QUERY"), true); |
| 129 | } |
| 130 | |
| 131 | // TODO: Listen for changes. |
| 132 | updateAvailableSlices(); |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 133 | if (TEST_INTENT) { |
| 134 | addSlice(new Intent("androidx.intent.SLICE_ACTION").setPackage(getPackageName())); |
| 135 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean onCreateOptionsMenu(Menu menu) { |
| 140 | mTypeMenu = menu.addSubMenu("Type"); |
| 141 | mTypeMenu.setIcon(R.drawable.ic_shortcut); |
| 142 | mTypeMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 143 | mTypeMenu.add("Shortcut"); |
| 144 | mTypeMenu.add("Small"); |
| 145 | mTypeMenu.add("Large"); |
Mady Mellor | c6620e7 | 2017-11-28 13:29:57 -0800 | [diff] [blame] | 146 | menu.add("Auth"); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 147 | super.onCreateOptionsMenu(menu); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean onOptionsItemSelected(MenuItem item) { |
| 153 | switch (item.getTitle().toString()) { |
Mady Mellor | c6620e7 | 2017-11-28 13:29:57 -0800 | [diff] [blame] | 154 | case "Auth": |
| 155 | authAllSlices(); |
| 156 | return true; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 157 | case "Shortcut": |
| 158 | mTypeMenu.setIcon(R.drawable.ic_shortcut); |
| 159 | mSelectedMode = SliceView.MODE_SHORTCUT; |
| 160 | updateSliceModes(); |
| 161 | return true; |
| 162 | case "Small": |
| 163 | mTypeMenu.setIcon(R.drawable.ic_small); |
| 164 | mSelectedMode = SliceView.MODE_SMALL; |
| 165 | updateSliceModes(); |
| 166 | return true; |
| 167 | case "Large": |
| 168 | mTypeMenu.setIcon(R.drawable.ic_large); |
| 169 | mSelectedMode = SliceView.MODE_LARGE; |
| 170 | updateSliceModes(); |
| 171 | return true; |
| 172 | } |
| 173 | return super.onOptionsItemSelected(item); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void onSaveInstanceState(Bundle outState) { |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 178 | super.onSaveInstanceState(outState); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 179 | outState.putInt("SELECTED_MODE", mSelectedMode); |
| 180 | outState.putString("SELECTED_QUERY", mSearchView.getQuery().toString()); |
| 181 | } |
| 182 | |
Mady Mellor | c6620e7 | 2017-11-28 13:29:57 -0800 | [diff] [blame] | 183 | private void authAllSlices() { |
| 184 | List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0); |
| 185 | packages.forEach(info -> { |
| 186 | for (int i = 0; i < URI_PATHS.length; i++) { |
| 187 | grantUriPermission(info.packageName, getUri(URI_PATHS[i], getApplicationContext()), |
| 188 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 189 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 190 | } |
| 191 | }); |
| 192 | } |
| 193 | |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 194 | private void updateAvailableSlices() { |
| 195 | mSliceUris.clear(); |
| 196 | List<PackageInfo> packageInfos = getPackageManager() |
| 197 | .getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); |
| 198 | for (PackageInfo pi : packageInfos) { |
| 199 | ActivityInfo[] activityInfos = pi.activities; |
| 200 | if (activityInfos != null) { |
| 201 | for (ActivityInfo ai : activityInfos) { |
| 202 | if (ai.metaData != null) { |
| 203 | String sliceUri = ai.metaData.getString(SLICE_METADATA_KEY); |
| 204 | if (sliceUri != null) { |
| 205 | mSliceUris.add(Uri.parse(sliceUri)); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
Mady Mellor | c133418 | 2017-11-10 15:50:35 -0800 | [diff] [blame] | 211 | for (int i = 0; i < URI_PATHS.length; i++) { |
| 212 | mSliceUris.add(getUri(URI_PATHS[i], getApplicationContext())); |
| 213 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 214 | populateAdapter(String.valueOf(mSearchView.getQuery())); |
| 215 | } |
| 216 | |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 217 | private void addSlice(Intent intent) { |
| 218 | SliceView v = new SliceView(getApplicationContext()); |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 219 | v.setSliceObserver(this); |
Jason Monk | ed97495 | 2017-11-27 13:48:04 -0500 | [diff] [blame] | 220 | v.setTag(intent); |
| 221 | if (mSliceLiveData != null) { |
| 222 | mSliceLiveData.removeObservers(this); |
| 223 | } |
| 224 | mContainer.removeAllViews(); |
| 225 | mContainer.addView(v); |
| 226 | mSliceLiveData = SliceLiveData.fromIntent(this, intent); |
| 227 | v.setMode(mSelectedMode); |
| 228 | mSliceLiveData.observe(this, v); |
| 229 | } |
| 230 | |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 231 | private void addSlice(Uri uri) { |
| 232 | if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
| 233 | SliceView v = new SliceView(getApplicationContext()); |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 234 | v.setSliceObserver(this); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 235 | v.setTag(uri); |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 236 | if (mSliceLiveData != null) { |
| 237 | mSliceLiveData.removeObservers(this); |
| 238 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 239 | mContainer.removeAllViews(); |
| 240 | mContainer.addView(v); |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 241 | mSliceLiveData = SliceLiveData.fromUri(this, uri); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 242 | v.setMode(mSelectedMode); |
Jason Monk | 698ad8c | 2017-11-10 10:31:19 -0500 | [diff] [blame] | 243 | mSliceLiveData.observe(this, v); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 244 | } else { |
| 245 | Log.w(TAG, "Invalid uri, skipping slice: " + uri); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private void updateSliceModes() { |
| 250 | final int count = mContainer.getChildCount(); |
| 251 | for (int i = 0; i < count; i++) { |
| 252 | ((SliceView) mContainer.getChildAt(i)).setMode(mSelectedMode); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | private void populateAdapter(String query) { |
| 257 | final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "uri"}); |
| 258 | ArrayMap<String, Integer> ranking = new ArrayMap<>(); |
| 259 | ArrayList<String> suggestions = new ArrayList(); |
| 260 | mSliceUris.forEach(uri -> { |
| 261 | String uriString = uri.toString(); |
| 262 | if (uriString.contains(query)) { |
| 263 | ranking.put(uriString, uriString.indexOf(query)); |
| 264 | suggestions.add(uriString); |
| 265 | } |
| 266 | }); |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 267 | suggestions.sort(new Comparator<String>() { |
| 268 | @Override |
| 269 | public int compare(String o1, String o2) { |
| 270 | return Integer.compare(ranking.get(o1), ranking.get(o2)); |
| 271 | } |
| 272 | }); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 273 | for (int i = 0; i < suggestions.size(); i++) { |
| 274 | c.addRow(new Object[]{i, suggestions.get(i)}); |
| 275 | } |
| 276 | mAdapter.changeCursor(c); |
| 277 | } |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 278 | |
| 279 | @Override |
| 280 | public void onSliceAction(@NonNull EventInfo info, @NonNull SliceItem item) { |
| 281 | Log.w(TAG, "onSliceAction, info: " + info); |
| 282 | Log.w(TAG, "onSliceAction, sliceItem: \n" + item); |
| 283 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 284 | } |