Improve shortcut demo
Bug 27548047
Change-Id: Iba717ec910e1fd48e623f5dea4604fc27e5d2818
diff --git a/samples/ShortcutDemo/common/res/layout/list_item.xml b/samples/ShortcutDemo/common/res/layout/list_item.xml
index c99cc66..9ec23c6 100644
--- a/samples/ShortcutDemo/common/res/layout/list_item.xml
+++ b/samples/ShortcutDemo/common/res/layout/list_item.xml
@@ -54,13 +54,14 @@
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
+ android:visibility="gone"
style="@android:style/Widget.Material.Button.Borderless"/>
<Button
- android:id="@+id/toggle"
- android:text="@string/toggle"
+ android:id="@+id/action2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
+ android:visibility="gone"
style="@android:style/Widget.Material.Button.Borderless"/>
</LinearLayout>
\ No newline at end of file
diff --git a/samples/ShortcutDemo/common/res/values/strings.xml b/samples/ShortcutDemo/common/res/values/strings.xml
index 9ec96f1..0d68c75 100644
--- a/samples/ShortcutDemo/common/res/values/strings.xml
+++ b/samples/ShortcutDemo/common/res/values/strings.xml
@@ -17,7 +17,4 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<add-resource type="string" name="launch"/>
<string name="launch">Launch</string>
-
- <add-resource type="string" name="toggle"/>
- <string name="toggle">Toggle</string>
</resources>
diff --git a/samples/ShortcutDemo/common/src/com/example/android/pm/shortcutdemo/ShortcutAdapter.java b/samples/ShortcutDemo/common/src/com/example/android/pm/shortcutdemo/ShortcutAdapter.java
index 4c28b1c..75dbc01 100644
--- a/samples/ShortcutDemo/common/src/com/example/android/pm/shortcutdemo/ShortcutAdapter.java
+++ b/samples/ShortcutDemo/common/src/com/example/android/pm/shortcutdemo/ShortcutAdapter.java
@@ -58,24 +58,24 @@
protected abstract int getText2Id();
protected abstract int getImageId();
protected abstract int getLaunchId();
- protected abstract int getToggleId();
+ protected abstract int getAction2Id();
- protected boolean showLaunch() {
+ protected boolean showLaunch(ShortcutInfo si) {
return false;
}
- protected boolean showToggle() {
+ protected boolean showAction2(ShortcutInfo si) {
return false;
}
- protected String getToggleText(ShortcutInfo si) {
- return "Toggle";
+ protected String getAction2Text(ShortcutInfo si) {
+ return "Action2";
}
protected void onLaunchClicked(ShortcutInfo si) {
}
- protected void onToggleClicked(ShortcutInfo si) {
+ protected void onAction2Clicked(ShortcutInfo si) {
}
public void setShortcuts(List<ShortcutInfo> shortcuts) {
@@ -132,14 +132,22 @@
}
public void bindView(View view, int position, ShortcutInfo si) {
- if (showLaunch()) {
+ {
final View v = view.findViewById(getLaunchId());
- v.setOnClickListener(this);
+ v.setVisibility(View.GONE);
+ if (showLaunch(si)) {
+ v.setOnClickListener(this);
+ v.setVisibility(View.VISIBLE);
+ }
}
- if (showToggle()) {
- final Button v = (Button) view.findViewById(getToggleId());
- v.setOnClickListener(this);
- v.setText(getToggleText(si));
+ {
+ final Button v = (Button) view.findViewById(getAction2Id());
+ v.setVisibility(View.GONE);
+ if (showAction2(si)) {
+ v.setOnClickListener(this);
+ v.setVisibility(View.VISIBLE);
+ v.setText(getAction2Text(si));
+ }
}
final TextView line1 = (TextView) view.findViewById(getText1Id());
@@ -197,8 +205,8 @@
final ShortcutInfo si = (ShortcutInfo)(((View) v.getParent()).getTag());
if (v.getId() == getLaunchId()) {
onLaunchClicked(si);
- } else if (v.getId() == getToggleId()) {
- onToggleClicked(si);
+ } else if (v.getId() == getAction2Id()) {
+ onAction2Clicked(si);
}
}
}
diff --git a/samples/ShortcutDemo/launcher/AndroidManifest.xml b/samples/ShortcutDemo/launcher/AndroidManifest.xml
index c3257bb..f75b8f9 100644
--- a/samples/ShortcutDemo/launcher/AndroidManifest.xml
+++ b/samples/ShortcutDemo/launcher/AndroidManifest.xml
@@ -21,7 +21,7 @@
<application android:label="@string/app_title"
android:resizeableActivity="true">
- <activity android:name="com.example.android.pm.shortcutlauncherdemo.Main">
+ <activity android:name="ShortcutLauncher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
diff --git a/samples/ShortcutDemo/launcher/res/layout/main.xml b/samples/ShortcutDemo/launcher/res/layout/main.xml
index bb24877..535ab6b 100644
--- a/samples/ShortcutDemo/launcher/res/layout/main.xml
+++ b/samples/ShortcutDemo/launcher/res/layout/main.xml
@@ -22,7 +22,6 @@
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
- android:choiceMode="multipleChoice"
android:enabled="true"
/>
</LinearLayout>
diff --git a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Main.java b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncher.java
similarity index 94%
rename from samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Main.java
rename to samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncher.java
index 20cd4ec..88d7317 100644
--- a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Main.java
+++ b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncher.java
@@ -28,8 +28,6 @@
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
-import android.view.View;
-import android.widget.ListView;
import com.example.android.pm.shortcutdemo.ShortcutAdapter;
@@ -38,7 +36,7 @@
import java.util.Comparator;
import java.util.List;
-public class Main extends ListActivity {
+public class ShortcutLauncher extends ListActivity {
public static final String TAG = "ShortcutLauncherDemo";
private LauncherApps mLauncherApps;
@@ -201,22 +199,22 @@
}
@Override
- protected int getToggleId() {
- return R.id.toggle;
+ protected int getAction2Id() {
+ return R.id.action2;
}
@Override
- protected boolean showLaunch() {
+ protected boolean showLaunch(ShortcutInfo si) {
return true;
}
@Override
- protected boolean showToggle() {
+ protected boolean showAction2(ShortcutInfo si) {
return true;
}
@Override
- protected String getToggleText(ShortcutInfo si) {
+ protected String getAction2Text(ShortcutInfo si) {
return si.isPinned() ? "Unpin" : "Pin";
}
@@ -226,7 +224,7 @@
}
@Override
- protected void onToggleClicked(ShortcutInfo si) {
+ protected void onAction2Clicked(ShortcutInfo si) {
togglePin(si);
}
}
diff --git a/samples/ShortcutDemo/publisher/Android.mk b/samples/ShortcutDemo/publisher/Android.mk
index bf0ac47..177c04c 100644
--- a/samples/ShortcutDemo/publisher/Android.mk
+++ b/samples/ShortcutDemo/publisher/Android.mk
@@ -29,7 +29,7 @@
LOCAL_AAPT_FLAGS += --rename-manifest-package com.example.android.pm.shortcutdemo
LOCAL_SRC_FILES := $(call all-java-files-under, src)
-#LOCAL_SRC_FILES += $(call all-java-files-under, ../common/src)
+LOCAL_SRC_FILES += $(call all-java-files-under, ../common/src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/../common/res
@@ -50,7 +50,7 @@
LOCAL_AAPT_FLAGS += --rename-manifest-package com.example.android.pm.shortcutdemo2
LOCAL_SRC_FILES := $(call all-java-files-under, src)
-#LOCAL_SRC_FILES += $(call all-java-files-under, ../common/src)
+LOCAL_SRC_FILES += $(call all-java-files-under, ../common/src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/../common/res
diff --git a/samples/ShortcutDemo/publisher/manifest1/AndroidManifest.xml b/samples/ShortcutDemo/publisher/manifest1/AndroidManifest.xml
index 1a3ed36..83171ea 100644
--- a/samples/ShortcutDemo/publisher/manifest1/AndroidManifest.xml
+++ b/samples/ShortcutDemo/publisher/manifest1/AndroidManifest.xml
@@ -24,7 +24,7 @@
<application android:label="@string/app_title"
android:resizeableActivity="true">
- <activity android:name="Main">
+ <activity android:name="ShortcutPublisher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
diff --git a/samples/ShortcutDemo/publisher/manifest2/AndroidManifest.xml b/samples/ShortcutDemo/publisher/manifest2/AndroidManifest.xml
index 887f379..724e27f 100644
--- a/samples/ShortcutDemo/publisher/manifest2/AndroidManifest.xml
+++ b/samples/ShortcutDemo/publisher/manifest2/AndroidManifest.xml
@@ -24,7 +24,7 @@
<application android:label="@string/app_title"
android:resizeableActivity="true">
- <activity android:name="Main">
+ <activity android:name="ShortcutPublisher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
diff --git a/samples/ShortcutDemo/publisher/res/layout/main.xml b/samples/ShortcutDemo/publisher/res/layout/main.xml
index f2dc492..0a9ef0f 100644
--- a/samples/ShortcutDemo/publisher/res/layout/main.xml
+++ b/samples/ShortcutDemo/publisher/res/layout/main.xml
@@ -18,17 +18,24 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
- android:id="@+id/publish"
- android:text="@string/publish"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="onPublishPressed"/>
+ android:id="@+id/publish"
+ android:text="@string/publish"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:onClick="onPublishPressed"/>
<Button
android:id="@+id/delete_all"
android:text="@string/delete_all"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onDeleteAllPressed"/>
+ <ListView
+ android:id="@android:id/list"
+ android:layout_width="match_parent"
+ android:layout_height="0dip"
+ android:layout_weight="1"
+ android:enabled="true"
+ />
</LinearLayout>
diff --git a/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/Main.java b/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/Main.java
deleted file mode 100644
index 7a687e8..0000000
--- a/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/Main.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Copyright (C) 2016 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.example.android.pm.shortcutdemo;
-
-import android.app.Activity;
-import android.app.WallpaperManager;
-import android.content.Intent;
-import android.content.pm.LauncherApps;
-import android.content.pm.ShortcutInfo;
-import android.content.pm.ShortcutManager;
-import android.graphics.BitmapFactory;
-import android.graphics.drawable.Icon;
-import android.net.Uri;
-import android.os.Bundle;
-import android.os.Environment;
-import android.os.ParcelFileDescriptor;
-import android.os.UserHandle;
-import android.os.Process;
-import android.util.Log;
-import android.view.View;
-import android.widget.Toast;
-
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-
-public class Main extends Activity {
- public static final String TAG = "ShortcutDemo";
-
- private ShortcutManager mShortcutManager;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.main);
-
- mShortcutManager = getSystemService(ShortcutManager.class);
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- }
-
- private void dumpCurrentShortcuts() {
- Log.d(TAG, "Dynamic shortcuts:");
- for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
- Log.d(TAG, " " + si.toString());
- }
- Log.d(TAG, "Pinned shortcuts:");
- for (ShortcutInfo si : mShortcutManager.getPinnedShortcuts()) {
- Log.d(TAG, " " + si.toString());
- }
- }
-
- private void showThrottledToast() {
- Toast.makeText(this,
- "Throttled, use \"adb shell cmd shortcut reset-throttling\" to reset counters",
- Toast.LENGTH_SHORT).show();
- }
-
- public void onPublishPressed(View view) {
- dumpCurrentShortcuts();
- final Icon icon1 = Icon.createWithResource(this, R.drawable.icon_large_1);
- final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(),
- R.drawable.icon_large_2));
- final Icon icon3 = Icon.createWithContentUri(
- Uri.parse("content://com.example.android.pm.shortcuts/" + R.drawable.icon_large_3));
-
- final Intent intent1 = new Intent(Intent.ACTION_VIEW);
- intent1.setClass(this, Main.class);
- intent1.putExtra("str", "str-value");
- intent1.putExtra("nest", new Bundle());
- intent1.getBundleExtra("nest").putInt("int", 123);
-
- final Intent intent2 = new Intent(Intent.ACTION_VIEW);
- intent2.setClass(this, Main.class);
- intent2.putExtra("str", "2");
-
- final Intent intent3 = new Intent(Intent.ACTION_VIEW);
- intent2.setClass(this, Main.class);
-
- final ShortcutInfo si1 = new ShortcutInfo.Builder(this)
- .setId("shortcut1")
- .setTitle("Title 1")
- .setIcon(icon1)
- .setWeight(10)
- .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")))
- .build();
-
- final ShortcutInfo si2 = new ShortcutInfo.Builder(this)
- .setId("shortcut2")
- .setTitle("Title 2")
- .setIcon(icon2)
- .setWeight(5)
- .setIntent(intent2)
- .build();
-
- final ShortcutInfo si3 = new ShortcutInfo.Builder(this)
- .setId("shortcut3")
- .setTitle("Title 3")
- .setIcon(icon3)
- .setWeight(15)
- .setIntent(intent3)
- .build();
-
- if (!mShortcutManager.setDynamicShortcuts(Arrays.asList(si1, si2, si3))) {
- showThrottledToast();
- }
- }
-
- public void onDeleteAllPressed(View view) {
- mShortcutManager.deleteAllDynamicShortcuts();
- }
-}
diff --git a/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/ShortcutPublisher.java b/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/ShortcutPublisher.java
new file mode 100644
index 0000000..008ea49
--- /dev/null
+++ b/samples/ShortcutDemo/publisher/src/com/example/android/pm/shortcutdemo/ShortcutPublisher.java
@@ -0,0 +1,247 @@
+/*
+ * Copyright (C) 2016 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.example.android.pm.shortcutdemo;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ShortcutInfo;
+import android.content.pm.ShortcutManager;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.os.Bundle;
+import android.util.ArrayMap;
+import android.util.Log;
+import android.view.View;
+import android.widget.ListView;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+public class ShortcutPublisher extends Activity {
+ public static final String TAG = "ShortcutDemo";
+
+ private ShortcutManager mShortcutManager;
+
+ private ListView mList;
+ private MyAdapter mAdapter;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.main);
+
+ mShortcutManager = getSystemService(ShortcutManager.class);
+
+ mList = (ListView) findViewById(android.R.id.list);
+ mAdapter = new MyAdapter(this);
+ mList.setAdapter(mAdapter);
+
+ Log.d(TAG, "extras=" + getIntent().getExtras());
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ refreshList();
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ }
+
+ private void refreshList() {
+ final Map<String, ShortcutInfo> map = new ArrayMap<>();
+ for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
+ if (!map.containsKey(si.getId())) {
+ map.put(si.getId(), si);
+ }
+ }
+ for (ShortcutInfo si : mShortcutManager.getPinnedShortcuts()) {
+ if (!map.containsKey(si.getId())) {
+ map.put(si.getId(), si);
+ }
+ }
+ final List<ShortcutInfo> list = new ArrayList<>(map.values());
+ Collections.sort(list, mShortcutComparator);
+ mAdapter.setShortcuts(list);
+ }
+
+ private final Comparator<ShortcutInfo> mShortcutComparator =
+ (ShortcutInfo s1, ShortcutInfo s2) -> {
+ int ret = 0;
+ ret = (s1.isDynamic() ? 0 : 1) - (s2.isDynamic() ? 0 : 1);
+ if (ret != 0) return ret;
+
+ ret = s1.getTitle().compareTo(s2.getTitle());
+ if (ret != 0) return ret;
+
+ ret = s1.getId().compareTo(s2.getId());
+ if (ret != 0) return ret;
+
+ return 0;
+ };
+
+ private void dumpCurrentShortcuts() {
+ Log.d(TAG, "Dynamic shortcuts:");
+ for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
+ Log.d(TAG, " " + si.toString());
+ }
+ Log.d(TAG, "Pinned shortcuts:");
+ for (ShortcutInfo si : mShortcutManager.getPinnedShortcuts()) {
+ Log.d(TAG, " " + si.toString());
+ }
+ }
+
+ private void showThrottledToast() {
+ Toast.makeText(this,
+ "Throttled, use \"adb shell cmd shortcut reset-throttling\" to reset counters",
+ Toast.LENGTH_SHORT).show();
+ }
+
+ public void onPublishPressed(View view) {
+ dumpCurrentShortcuts();
+ final Icon icon1 = Icon.createWithResource(this, R.drawable.icon_large_1);
+ final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(),
+ R.drawable.icon_large_2));
+ final Icon icon3 = Icon.createWithContentUri(
+ Uri.parse("content://" + getPackageName() + "/" + R.drawable.icon_large_3));
+
+ final Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
+
+ final Intent intent2 = new Intent(Intent.ACTION_VIEW);
+ intent2.setClass(this, ShortcutPublisher.class);
+
+ final Intent intent3 = new Intent(Intent.ACTION_VIEW);
+ intent3.setClass(this, ShortcutPublisher.class);
+ intent3.putExtra("str", "str-value");
+ intent3.putExtra("nest", new Bundle());
+ intent3.getBundleExtra("nest").putInt("int", 123);
+
+ final ShortcutInfo si1 = new ShortcutInfo.Builder(this)
+ .setId("shortcut1")
+ .setTitle("Google Search")
+ .setIcon(icon1)
+ .setWeight(10)
+ .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")))
+ .build();
+
+ final ShortcutInfo si2 = new ShortcutInfo.Builder(this)
+ .setId("shortcut2")
+ .setTitle("Shortcut Demo Main")
+ .setIcon(icon2)
+ .setWeight(5)
+ .setIntent(intent2)
+ .build();
+
+ final ShortcutInfo si3 = new ShortcutInfo.Builder(this)
+ .setId("shortcut3")
+ .setTitle("Shortcut Demo Main with extras")
+ .setIcon(icon3)
+ .setWeight(15)
+ .setIntent(intent3)
+ .build();
+
+ if (!mShortcutManager.setDynamicShortcuts(Arrays.asList(si1, si2, si3))) {
+ showThrottledToast();
+ }
+ refreshList();
+ }
+
+ public void onDeleteAllPressed(View view) {
+ mShortcutManager.deleteAllDynamicShortcuts();
+ refreshList();
+ }
+
+ void launch(ShortcutInfo si) {
+ startActivity(si.getIntent());
+ }
+
+ void deleteDynamic(ShortcutInfo si) {
+ mShortcutManager.deleteDynamicShortcut(si.getId());
+ refreshList();
+ }
+
+ class MyAdapter extends ShortcutAdapter {
+ public MyAdapter(Context context) {
+ super(context);
+ }
+
+ @Override
+ protected int getLayoutId() {
+ return R.layout.list_item;
+ }
+
+ @Override
+ protected int getText1Id() {
+ return R.id.line1;
+ }
+
+ @Override
+ protected int getText2Id() {
+ return R.id.line2;
+ }
+
+ @Override
+ protected int getImageId() {
+ return R.id.image;
+ }
+
+ @Override
+ protected int getLaunchId() {
+ return R.id.launch;
+ }
+
+ @Override
+ protected int getAction2Id() {
+ return R.id.action2;
+ }
+
+ @Override
+ protected boolean showLaunch(ShortcutInfo si) {
+ return true;
+ }
+
+ @Override
+ protected boolean showAction2(ShortcutInfo si) {
+ return si.isDynamic();
+ }
+
+ @Override
+ protected String getAction2Text(ShortcutInfo si) {
+ return "Delete Dynamic";
+ }
+
+ @Override
+ protected void onLaunchClicked(ShortcutInfo si) {
+ launch(si);
+ }
+
+ @Override
+ protected void onAction2Clicked(ShortcutInfo si) {
+ deleteDynamic(si);
+ }
+ }
+}