diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-11-01 08:13:49 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-11-01 08:13:49 +0000 |
commit | 7012495833eb8d13b78f3931f4ea0dfa380b4d63 (patch) | |
tree | 3c1dbc399eaa1938ffa04311a47da25acc957a7a | |
parent | f97694de09a3561671bf2f1ba6c64179be1f34f4 (diff) | |
parent | 02c6b42c24587e5d1020c22697e4a74f3e2b4925 (diff) | |
download | experimental-7012495833eb8d13b78f3931f4ea0dfa380b4d63.tar.gz |
Snap for 4428213 from 02c6b42c24587e5d1020c22697e4a74f3e2b4925 to pi-release
Change-Id: I6a10f82faa9e0d2820c3976e58a17ab4c27368c8
-rw-r--r-- | SliceBrowser/Android.mk (renamed from SlicePresenter/Android.mk) | 7 | ||||
-rw-r--r-- | SliceBrowser/AndroidManifest.xml (renamed from SlicePresenter/AndroidManifest.xml) | 3 | ||||
-rw-r--r-- | SliceBrowser/res/drawable/ic_large.xml | 28 | ||||
-rw-r--r-- | SliceBrowser/res/drawable/ic_shortcut.xml | 34 | ||||
-rw-r--r-- | SliceBrowser/res/drawable/ic_small.xml | 27 | ||||
-rw-r--r-- | SliceBrowser/res/layout/activity_layout.xml | 75 | ||||
-rw-r--r-- | SliceBrowser/res/values/strings.xml (renamed from SlicePresenter/res/values/strings.xml) | 2 | ||||
-rw-r--r-- | SliceBrowser/src/com/android/experimental/slicepresenter/SlicePresenter.java | 222 | ||||
-rw-r--r-- | SlicePresenter/res/layout/activity_layout.xml | 59 | ||||
-rw-r--r-- | SlicePresenter/src/com/android/experimental/slicepresenter/SlicePresenter.java | 176 |
10 files changed, 395 insertions, 238 deletions
diff --git a/SlicePresenter/Android.mk b/SliceBrowser/Android.mk index 19a5b74..578f7b1 100644 --- a/SlicePresenter/Android.mk +++ b/SliceBrowser/Android.mk @@ -16,9 +16,14 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) +LOCAL_USE_AAPT2 := true + LOCAL_PACKAGE_NAME := SlicePresenter LOCAL_SRC_FILES := $(call all-java-files-under, src) \ - $(call all-Iaidl-files-under, src) + $(call all-Iaidl-files-under, src) + +LOCAL_STATIC_ANDROID_LIBRARIES := \ + android-support-v7-cardview LOCAL_CERTIFICATE := platform diff --git a/SlicePresenter/AndroidManifest.xml b/SliceBrowser/AndroidManifest.xml index fff34b2..0e15bfd 100644 --- a/SlicePresenter/AndroidManifest.xml +++ b/SliceBrowser/AndroidManifest.xml @@ -24,7 +24,8 @@ <uses-permission android:name="android.permission.BIND_SLICE" /> <application android:label="@string/app_label"> - <activity android:name=".SlicePresenter"> + <activity android:name=".SlicePresenter" + android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> diff --git a/SliceBrowser/res/drawable/ic_large.xml b/SliceBrowser/res/drawable/ic_large.xml new file mode 100644 index 0000000..b8042a8 --- /dev/null +++ b/SliceBrowser/res/drawable/ic_large.xml @@ -0,0 +1,28 @@ +<!-- +Copyright (C) 2014 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="48.0dp" + android:height="48.0dp" + android:viewportWidth="48.0" + android:viewportHeight="48.0"> + <path + android:fillColor="#FF000000" + android:pathData="M34.0,2.02L14.0,2.0c-2.21,0.0 -4.0,1.79 -4.0,4.0l0.0,36.0c0.0,2.21 1.79,4.0 4.0,4.0l20.0,0.0c2.21,0.0 4.0,-1.79 4.0,-4.0L38.0,6.0c0.0,-2.21 -1.79,-3.98 -4.0,-3.98zM34.0,38.0L14.0,38.0L14.0,10.0l20.0,0.0l0.0,28.0z"/> + <path + android:strokeColor="#FF000000" + android:strokeWidth="2" + android:pathData="M16,18 l16,0 l0,10 l-16,0z"/> +</vector> diff --git a/SliceBrowser/res/drawable/ic_shortcut.xml b/SliceBrowser/res/drawable/ic_shortcut.xml new file mode 100644 index 0000000..4b5485d --- /dev/null +++ b/SliceBrowser/res/drawable/ic_shortcut.xml @@ -0,0 +1,34 @@ +<!-- +Copyright (C) 2014 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:height="48.0dp" + android:viewportHeight="48.0" + android:viewportWidth="48.0" + android:width="48.0dp"> + <path + android:fillColor="#e2e2e2" + android:pathData="M24.0,24.0m-19.0,0.0a19.0,19.0 0.0,1.0 1.0,38.0 0.0a19.0,19.0 0.0,1.0 1.0,-38.0 0.0"/> + <group + android:scaleX=".7" + android:scaleY=".7" + android:translateX="7.2" + android:translateY="7.2"> + + <path + android:fillColor="#ff000000" + android:pathData="M12.0,36.0c0.0,1.0 0.9,2.0 2.0,2.0l2.0,0.0l0.0,7.0c0.0,1.66 1.34,3.0 3.0,3.0s3.0,-1.34 3.0,-3.0l0.0,-7.0l4.0,0.0l0.0,7.0c0.0,1.66 1.34,3.0 3.0,3.0s3.0,-1.34 3.0,-3.0l0.0,-7.0l2.0,0.0c1.1,0.0 2.0,-0.9 2.0,-2.0L36.0,16.0L12.0,16.0l0.0,20.0zM7.0,16.0c-1.66,0.0 -3.0,1.34 -3.0,3.0l0.0,14.0c0.0,1.66 1.34,3.0 3.0,3.0s3.0,-1.34 3.0,-3.0L10.0,19.0c0.0,-1.66 -1.34,-3.0 -3.0,-3.0zm34.0,0.0c-1.66,0.0 -3.0,1.34 -3.0,3.0l0.0,14.0c0.0,1.66 1.34,3.0 3.0,3.0s3.0,-1.34 3.0,-3.0L44.0,19.0c0.0,-1.66 -1.34,-3.0 -3.0,-3.0zM31.06,4.32l2.61,-2.61c0.39,-0.3 0.39,-1.02 0.0,-1.41 -0.39,-0.39 -1.02,-0.39 -1.41,0.0L29.3,3.25C27.7,2.46 25.91,2.0 24.0,2.0c-1.92,0.0 -3.7,0.46 -5.33,1.26L15.0,0.29c-0.39,-0.39 -1.02,-0.39 -1.41,0.0 -0.3,0.39 -0.39,1.02 0.0,1.41l2.62,2.62C13.94,6.51 12.0,10.03 12.0,14.0l24.0,0.0c0.0,-3.98 -1.95,-7.5 -4.94,-9.68zM20.0,10.0l-2.0,0.0L18.0,8.0l2.0,0.0l0.0,2.0zm10.0,0.0l-2.0,0.0L28.0,8.0l2.0,0.0l0.0,2.0z"/> + </group> +</vector> diff --git a/SliceBrowser/res/drawable/ic_small.xml b/SliceBrowser/res/drawable/ic_small.xml new file mode 100644 index 0000000..b35e1c1 --- /dev/null +++ b/SliceBrowser/res/drawable/ic_small.xml @@ -0,0 +1,27 @@ +<!-- +Copyright (C) 2014 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="48.0dp" + android:height="48.0dp" + android:viewportWidth="48.0" + android:viewportHeight="48.0"> + <path + android:fillColor="#FF000000" + android:pathData="M34.0,2.02L14.0,2.0c-2.21,0.0 -4.0,1.79 -4.0,4.0l0.0,36.0c0.0,2.21 1.79,4.0 4.0,4.0l20.0,0.0c2.21,0.0 4.0,-1.79 4.0,-4.0L38.0,6.0c0.0,-2.21 -1.79,-3.98 -4.0,-3.98zM34.0,38.0L14.0,38.0L14.0,10.0l20.0,0.0l0.0,28.0z"/> + <path + android:fillColor="#FF000000" + android:pathData="M16,18 l16,0 l0,4 l-16,0z"/> +</vector> diff --git a/SliceBrowser/res/layout/activity_layout.xml b/SliceBrowser/res/layout/activity_layout.xml new file mode 100644 index 0000000..833414c --- /dev/null +++ b/SliceBrowser/res/layout/activity_layout.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:gravity="top" + android:orientation="vertical" > + + <FrameLayout + android:id="@+id/search_bar_container" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:background="#f2f2f2"> + <android.support.v7.widget.CardView + android:id="@+id/search_bar" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="8dp" + app:cardCornerRadius="2dp" + app:cardBackgroundColor="?android:attr/colorBackground" + app:cardElevation="2dp"> + <Toolbar + android:id="@+id/search_toolbar" + android:layout_width="match_parent" + android:layout_height="48dp" + android:background="?android:attr/selectableItemBackground" + android:contentInsetStart="0dp" + android:contentInsetStartWithNavigation="0dp" + android:theme="?android:attr/actionBarTheme"> + <SearchView + android:id="@+id/search_view" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:iconifiedByDefault="false" + android:imeOptions="actionSearch|flagNoExtractUi" + android:queryHint="content://..." + android:searchIcon="@null"/> + </Toolbar> + </android.support.v7.widget.CardView> + </FrameLayout> + + + <ScrollView + android:id="@+id/container" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" > + + <FrameLayout + android:id="@+id/slice_preview" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_margin="8dp" + android:orientation="vertical" + android:background="?android:attr/colorBackground" + android:elevation="10dp" /> + </ScrollView> + +</LinearLayout> diff --git a/SlicePresenter/res/values/strings.xml b/SliceBrowser/res/values/strings.xml index baf6e3a..3b5c795 100644 --- a/SlicePresenter/res/values/strings.xml +++ b/SliceBrowser/res/values/strings.xml @@ -14,7 +14,7 @@ limitations under the License. --> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> - <string name="app_label">Slice Presenter</string> + <string name="app_label">Slice Browser</string> <string name="intent_button">Use Intent Slice</string> <string name="uri_button">Use URI Slice</string> diff --git a/SliceBrowser/src/com/android/experimental/slicepresenter/SlicePresenter.java b/SliceBrowser/src/com/android/experimental/slicepresenter/SlicePresenter.java new file mode 100644 index 0000000..6b8a90e --- /dev/null +++ b/SliceBrowser/src/com/android/experimental/slicepresenter/SlicePresenter.java @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.android.experimental.slicepresenter; + +import android.app.Activity; +import android.app.slice.Slice; +import android.app.slice.widget.SliceView; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.database.MatrixCursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.BaseColumns; +import android.util.ArrayMap; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; +import android.view.SubMenu; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CursorAdapter; +import android.widget.LinearLayout; +import android.widget.SearchView; +import android.widget.SearchView.OnSuggestionListener; +import android.widget.SimpleCursorAdapter; +import android.widget.Spinner; +import android.widget.Toolbar; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class SlicePresenter extends Activity { + + private static final String TAG = "SlicePresenter"; + + private static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; + + private ArrayList<Uri> mSliceUris = new ArrayList<Uri>(); + private String mSelectedMode; + private ViewGroup mContainer; + private boolean mShowingIntentSlice; + private SearchView mSearchView; + private SimpleCursorAdapter mAdapter; + private SubMenu mTypeMenu; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_layout); + + Toolbar toolbar = findViewById(R.id.search_toolbar); + setActionBar(toolbar); + + // Shows the slice + mContainer = findViewById(R.id.slice_preview); + mSearchView = findViewById(R.id.search_view); + + final String[] from = new String[]{"uri"}; + final int[] to = new int[]{android.R.id.text1}; + mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, + null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); + mSearchView.setSuggestionsAdapter(mAdapter); + mSearchView.setIconifiedByDefault(false); + mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { + @Override + public boolean onSuggestionClick(int position) { + mSearchView.setQuery(((Cursor) mAdapter.getItem(position)).getString(1), true); + return true; + } + + @Override + public boolean onSuggestionSelect(int position) { + mSearchView.setQuery(((Cursor) mAdapter.getItem(position)).getString(1), true); + return true; + } + }); + mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String s) { + addSlice(Uri.parse(s)); + mSearchView.clearFocus(); + return false; + } + + @Override + public boolean onQueryTextChange(String s) { + populateAdapter(s); + return false; + } + }); + + mSelectedMode = (savedInstanceState != null) + ? savedInstanceState.getString("SELECTED_MODE", SliceView.MODE_SHORTCUT) + : SliceView.MODE_SHORTCUT; + if (savedInstanceState != null) { + mSearchView.setQuery(savedInstanceState.getString("SELECTED_QUERY"), true); + } + + // TODO: Listen for changes. + updateAvailableSlices(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + mTypeMenu = menu.addSubMenu("Type"); + mTypeMenu.setIcon(R.drawable.ic_shortcut); + mTypeMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); + mTypeMenu.add("Shortcut"); + mTypeMenu.add("Small"); + mTypeMenu.add("Large"); + super.onCreateOptionsMenu(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getTitle().toString()) { + case "Shortcut": + mTypeMenu.setIcon(R.drawable.ic_shortcut); + mSelectedMode = SliceView.MODE_SHORTCUT; + updateSliceModes(); + return true; + case "Small": + mTypeMenu.setIcon(R.drawable.ic_small); + mSelectedMode = SliceView.MODE_SMALL; + updateSliceModes(); + return true; + case "Large": + mTypeMenu.setIcon(R.drawable.ic_large); + mSelectedMode = SliceView.MODE_LARGE; + updateSliceModes(); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + outState.putString("SELECTED_MODE", mSelectedMode); + outState.putString("SELECTED_QUERY", mSearchView.getQuery().toString()); + } + + private void updateAvailableSlices() { + mSliceUris.clear(); + List<PackageInfo> packageInfos = getPackageManager() + .getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); + for (PackageInfo pi : packageInfos) { + ActivityInfo[] activityInfos = pi.activities; + if (activityInfos != null) { + for (ActivityInfo ai : activityInfos) { + if (ai.metaData != null) { + String sliceUri = ai.metaData.getString(SLICE_METADATA_KEY); + if (sliceUri != null) { + mSliceUris.add(Uri.parse(sliceUri)); + } + } + } + } + } + populateAdapter(String.valueOf(mSearchView.getQuery())); + } + + private void addSlice(Uri uri) { + if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { + SliceView v = new SliceView(getApplicationContext()); + v.setTag(uri); + mContainer.removeAllViews(); + mContainer.addView(v); + v.setMode(mSelectedMode); + v.setSlice(uri); + } else { + Log.w(TAG, "Invalid uri, skipping slice: " + uri); + } + } + + private void updateSliceModes() { + final int count = mContainer.getChildCount(); + for (int i = 0; i < count; i++) { + ((SliceView) mContainer.getChildAt(i)).setMode(mSelectedMode); + } + } + + private void populateAdapter(String query) { + final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "uri"}); + ArrayMap<String, Integer> ranking = new ArrayMap<>(); + ArrayList<String> suggestions = new ArrayList(); + mSliceUris.forEach(uri -> { + String uriString = uri.toString(); + if (uriString.contains(query)) { + ranking.put(uriString, uriString.indexOf(query)); + suggestions.add(uriString); + } + }); + suggestions.sort(Comparator.comparingInt(ranking::get)); + for (int i = 0; i < suggestions.size(); i++) { + c.addRow(new Object[]{i, suggestions.get(i)}); + } + mAdapter.changeCursor(c); + } +} diff --git a/SlicePresenter/res/layout/activity_layout.xml b/SlicePresenter/res/layout/activity_layout.xml deleted file mode 100644 index 98a838b..0000000 --- a/SlicePresenter/res/layout/activity_layout.xml +++ /dev/null @@ -1,59 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- - Copyright (C) 2014 The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---> - -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:gravity="top" - android:orientation="vertical" > - - <ScrollView - android:id="@+id/container" - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_weight="1" > - - <FrameLayout - android:id="@+id/slice_preview" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:orientation="vertical" /> - </ScrollView> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_gravity="bottom" - android:layout_margin="16dp" - android:layout_weight="0.25" - android:orientation="horizontal" > - - <Spinner - android:id="@+id/spinner" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:padding="8dp" /> - - <Button - android:id="@+id/slice_button" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:padding="8dp" - android:text="@string/intent_button" /> - </LinearLayout> - -</LinearLayout>
\ No newline at end of file diff --git a/SlicePresenter/src/com/android/experimental/slicepresenter/SlicePresenter.java b/SlicePresenter/src/com/android/experimental/slicepresenter/SlicePresenter.java deleted file mode 100644 index e6d6550..0000000 --- a/SlicePresenter/src/com/android/experimental/slicepresenter/SlicePresenter.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -package com.android.experimental.slicepresenter; - -import android.app.Activity; -import android.app.slice.Slice; -import android.app.slice.widget.SliceView; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemSelectedListener; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.LinearLayout; -import android.widget.Spinner; - -import java.util.ArrayList; -import java.util.List; - -public class SlicePresenter extends Activity { - - private static final String TAG = "SlicePresenter"; - - private static final String SLICE_METADATA_KEY = "android.metadata.SLICE_URI"; - - private ArrayList<Uri> mSliceUris = new ArrayList<Uri>(); - private String mSelectedMode; - private ViewGroup mContainer; - private boolean mShowingIntentSlice; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_layout); - - // Shows the slice - mContainer = (ViewGroup) findViewById(R.id.slice_preview); - - // Select the slice mode - List<String> list = new ArrayList<>(); - list.add(SliceView.MODE_SHORTCUT); - list.add(SliceView.MODE_SMALL); - list.add(SliceView.MODE_LARGE); - Spinner spinner = (Spinner) findViewById(R.id.spinner); - ArrayAdapter<String> adapter = new ArrayAdapter<>(this, - android.R.layout.simple_spinner_item, list); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - spinner.setAdapter(adapter); - mSelectedMode = (savedInstanceState != null) - ? savedInstanceState.getString("SELECTED_MODE", list.get(0)) - : list.get(0); - spinner.setSelection(list.indexOf(mSelectedMode)); - spinner.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { - mSelectedMode = list.get(position); - updateSliceModes(); - } - - @Override - public void onNothingSelected(AdapterView<?> parent) { - } - }); - - // Toggle slice between URI based or Intent based - final String intentButton = getResources().getString(R.string.intent_button); - final String uriButton = getResources().getString(R.string.uri_button); - Button button = (Button) findViewById(R.id.slice_button); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final String state = (String) button.getText(); - final String newState = state.equals(intentButton) ? uriButton : intentButton; - button.setText(newState); - mShowingIntentSlice = newState.equals(uriButton); - if (mShowingIntentSlice) { - showIntentSlice(); - } else { - showAvailableSlices(); - } - } - }); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - outState.putString("SELECTED_MODE", mSelectedMode); - } - - @Override - public void onResume() { - super.onResume(); - if (mShowingIntentSlice) { - showIntentSlice(); - } else { - showAvailableSlices(); - } - } - - private void showAvailableSlices() { - mContainer.removeAllViews(); - mSliceUris.clear(); - List<PackageInfo> packageInfos = getPackageManager() - .getInstalledPackages(PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); - for (PackageInfo pi : packageInfos) { - ActivityInfo[] activityInfos = pi.activities; - if (activityInfos != null) { - for (ActivityInfo ai : activityInfos) { - if (ai.metaData != null) { - String sliceUri = ai.metaData.getString(SLICE_METADATA_KEY); - if (sliceUri != null) { - mSliceUris.add(Uri.parse(sliceUri)); - } - } - } - } - } - for (int i = 0; i < mSliceUris.size(); i++) { - addSlice(mSliceUris.get(i)); - } - } - - private void addSlice(Uri uri) { - if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { - SliceView v = new SliceView(getApplicationContext()); - v.setTag(uri); - mContainer.addView(v); - v.setMode(mSelectedMode); - v.setSlice(uri); - } else { - Log.w(TAG, "Invalid uri, skipping slice: " + uri); - } - } - - private void showIntentSlice() { - Intent intent = new Intent("android.intent.action.EXAMPLE_ACTION"); - mContainer.removeAllViews(); - SliceView v = new SliceView(getApplicationContext()); - v.setTag(intent); - /* - boolean added = v.setSlice(intent); - if (added) { - mContainer.addView(v); - v.setMode(mSelectedMode); - } - */ - } - - private void updateSliceModes() { - final int count = mContainer.getChildCount(); - for (int i = 0; i < count; i++) { - ((SliceView) mContainer.getChildAt(i)).setMode(mSelectedMode); - } - } -} |