Shortcut Demo: update shortcuts
Bug 27548047 Change-Id: I354cd468217f735dd79407de59a855cace247a85
This commit is contained in:
@@ -47,6 +47,14 @@
|
|||||||
android:textColor="#444444"
|
android:textColor="#444444"
|
||||||
/>
|
/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
<Button
|
||||||
|
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"/>
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/launch"
|
android:id="@+id/launch"
|
||||||
android:text="@string/launch"
|
android:text="@string/launch"
|
||||||
@@ -56,12 +64,4 @@
|
|||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
style="@android:style/Widget.Material.Button.Borderless"/>
|
style="@android:style/Widget.Material.Button.Borderless"/>
|
||||||
<Button
|
|
||||||
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>
|
</LinearLayout>
|
||||||
@@ -129,9 +129,6 @@ public class ShortcutLauncher extends ListActivity {
|
|||||||
ret = getAppLabel(s1.getPackageName()).compareTo(getAppLabel(s2.getPackageName()));
|
ret = getAppLabel(s1.getPackageName()).compareTo(getAppLabel(s2.getPackageName()));
|
||||||
if (ret != 0) return ret;
|
if (ret != 0) return ret;
|
||||||
|
|
||||||
ret = s1.getTitle().compareTo(s2.getTitle());
|
|
||||||
if (ret != 0) return ret;
|
|
||||||
|
|
||||||
ret = s1.getId().compareTo(s2.getId());
|
ret = s1.getId().compareTo(s2.getId());
|
||||||
if (ret != 0) return ret;
|
if (ret != 0) return ret;
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,18 @@
|
|||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:onClick="onPublishPressed"/>
|
android:onClick="onPublishPressed"/>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/add_shortcut"
|
||||||
|
android:text="@string/add_shortcut"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:onClick="onAddPressed"/>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/update_shortcuts"
|
||||||
|
android:text="@string/update_shortcuts"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:onClick="onUpdatePressed"/>
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/delete_all"
|
android:id="@+id/delete_all"
|
||||||
android:text="@string/delete_all"
|
android:text="@string/delete_all"
|
||||||
|
|||||||
@@ -18,6 +18,12 @@
|
|||||||
<add-resource type="string" name="publish"/>
|
<add-resource type="string" name="publish"/>
|
||||||
<string name="publish">Publish shortcuts</string>
|
<string name="publish">Publish shortcuts</string>
|
||||||
|
|
||||||
|
<add-resource type="string" name="add_shortcut"/>
|
||||||
|
<string name="add_shortcut">Add shortcut</string>
|
||||||
|
|
||||||
|
<add-resource type="string" name="update_shortcuts"/>
|
||||||
|
<string name="update_shortcuts">Update shortcuts</string>
|
||||||
|
|
||||||
<add-resource type="string" name="delete_all"/>
|
<add-resource type="string" name="delete_all"/>
|
||||||
<string name="delete_all">Delete all dynamic shortcuts</string>
|
<string name="delete_all">Delete all dynamic shortcuts</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -24,8 +24,10 @@ import android.graphics.BitmapFactory;
|
|||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.format.Time;
|
||||||
import android.util.ArrayMap;
|
import android.util.ArrayMap;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.Pair;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@@ -36,6 +38,8 @@ import java.util.Collections;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
public class ShortcutPublisher extends Activity {
|
public class ShortcutPublisher extends Activity {
|
||||||
public static final String TAG = "ShortcutDemo";
|
public static final String TAG = "ShortcutDemo";
|
||||||
@@ -45,6 +49,8 @@ public class ShortcutPublisher extends Activity {
|
|||||||
private ListView mList;
|
private ListView mList;
|
||||||
private MyAdapter mAdapter;
|
private MyAdapter mAdapter;
|
||||||
|
|
||||||
|
private final Random mRandom = new Random();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -72,7 +78,7 @@ public class ShortcutPublisher extends Activity {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshList() {
|
private List<ShortcutInfo> getAllShortcuts() {
|
||||||
final Map<String, ShortcutInfo> map = new ArrayMap<>();
|
final Map<String, ShortcutInfo> map = new ArrayMap<>();
|
||||||
for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
|
for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
|
||||||
if (!map.containsKey(si.getId())) {
|
if (!map.containsKey(si.getId())) {
|
||||||
@@ -84,7 +90,11 @@ public class ShortcutPublisher extends Activity {
|
|||||||
map.put(si.getId(), si);
|
map.put(si.getId(), si);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final List<ShortcutInfo> list = new ArrayList<>(map.values());
|
return new ArrayList<>(map.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshList() {
|
||||||
|
final List<ShortcutInfo> list = getAllShortcuts();
|
||||||
Collections.sort(list, mShortcutComparator);
|
Collections.sort(list, mShortcutComparator);
|
||||||
mAdapter.setShortcuts(list);
|
mAdapter.setShortcuts(list);
|
||||||
}
|
}
|
||||||
@@ -95,9 +105,6 @@ public class ShortcutPublisher extends Activity {
|
|||||||
ret = (s1.isDynamic() ? 0 : 1) - (s2.isDynamic() ? 0 : 1);
|
ret = (s1.isDynamic() ? 0 : 1) - (s2.isDynamic() ? 0 : 1);
|
||||||
if (ret != 0) return ret;
|
if (ret != 0) return ret;
|
||||||
|
|
||||||
ret = s1.getTitle().compareTo(s2.getTitle());
|
|
||||||
if (ret != 0) return ret;
|
|
||||||
|
|
||||||
ret = s1.getId().compareTo(s2.getId());
|
ret = s1.getId().compareTo(s2.getId());
|
||||||
if (ret != 0) return ret;
|
if (ret != 0) return ret;
|
||||||
|
|
||||||
@@ -115,22 +122,51 @@ public class ShortcutPublisher extends Activity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showToast(String message) {
|
||||||
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
private void showThrottledToast() {
|
private void showThrottledToast() {
|
||||||
Toast.makeText(this,
|
showToast("Throttled, use \"adb shell cmd shortcut reset-throttling\" to reset counters");
|
||||||
"Throttled, use \"adb shell cmd shortcut reset-throttling\" to reset counters",
|
}
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
|
private void callApi(BooleanSupplier call) {
|
||||||
|
try {
|
||||||
|
if (!call.getAsBoolean()) {
|
||||||
|
showThrottledToast();
|
||||||
|
}
|
||||||
|
refreshList();
|
||||||
|
} catch (RuntimeException r) {
|
||||||
|
Log.w(TAG, r.getMessage(), r);
|
||||||
|
showToast(r.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pair<String, String>> mIntentList = Arrays.asList(
|
||||||
|
Pair.create("Google Search", "http://www.google.com"),
|
||||||
|
Pair.create("Google Mail", "http://mail.google.com"),
|
||||||
|
Pair.create("Google Maps", "http://maps.google.com"),
|
||||||
|
Pair.create("Google Drive", "http://drive.google.com"),
|
||||||
|
Pair.create("Google Photos", "http://photos.google.com"),
|
||||||
|
Pair.create("Google Hangouts", "http://hangouts.google.com"),
|
||||||
|
Pair.create("Google+", "http://plus.google.com")
|
||||||
|
);
|
||||||
|
|
||||||
|
public ShortcutInfo.Builder addRandomIntents(ShortcutInfo.Builder b) {
|
||||||
|
final int i = mRandom.nextInt(mIntentList.size());
|
||||||
|
b.setTitle(mIntentList.get(i).first);
|
||||||
|
b.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(mIntentList.get(i).second)));
|
||||||
|
b.setIcon(Icon.createWithResource(this, R.drawable.icon2));
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPublishPressed(View view) {
|
public void onPublishPressed(View view) {
|
||||||
dumpCurrentShortcuts();
|
dumpCurrentShortcuts();
|
||||||
final Icon icon1 = Icon.createWithResource(this, R.drawable.icon_large_1);
|
|
||||||
final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(),
|
final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(),
|
||||||
R.drawable.icon_large_2));
|
R.drawable.icon_large_2));
|
||||||
final Icon icon3 = Icon.createWithContentUri(
|
final Icon icon3 = Icon.createWithContentUri(
|
||||||
Uri.parse("content://" + getPackageName() + "/" + R.drawable.icon_large_3));
|
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);
|
final Intent intent2 = new Intent(Intent.ACTION_VIEW);
|
||||||
intent2.setClass(this, ShortcutPublisher.class);
|
intent2.setClass(this, ShortcutPublisher.class);
|
||||||
|
|
||||||
@@ -140,13 +176,9 @@ public class ShortcutPublisher extends Activity {
|
|||||||
intent3.putExtra("nest", new Bundle());
|
intent3.putExtra("nest", new Bundle());
|
||||||
intent3.getBundleExtra("nest").putInt("int", 123);
|
intent3.getBundleExtra("nest").putInt("int", 123);
|
||||||
|
|
||||||
final ShortcutInfo si1 = new ShortcutInfo.Builder(this)
|
final ShortcutInfo si1 = addRandomIntents(new ShortcutInfo.Builder(this)
|
||||||
.setId("shortcut1")
|
.setId("shortcut1")
|
||||||
.setTitle("Google Search")
|
.setWeight(10)).build();
|
||||||
.setIcon(icon1)
|
|
||||||
.setWeight(10)
|
|
||||||
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
final ShortcutInfo si2 = new ShortcutInfo.Builder(this)
|
final ShortcutInfo si2 = new ShortcutInfo.Builder(this)
|
||||||
.setId("shortcut2")
|
.setId("shortcut2")
|
||||||
@@ -164,15 +196,56 @@ public class ShortcutPublisher extends Activity {
|
|||||||
.setIntent(intent3)
|
.setIntent(intent3)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
if (!mShortcutManager.setDynamicShortcuts(Arrays.asList(si1, si2, si3))) {
|
callApi(new BooleanSupplier() {
|
||||||
showThrottledToast();
|
@Override
|
||||||
|
public boolean getAsBoolean() {
|
||||||
|
return mShortcutManager.setDynamicShortcuts(Arrays.asList(si1, si2, si3));
|
||||||
}
|
}
|
||||||
refreshList();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDeleteAllPressed(View view) {
|
public void onDeleteAllPressed(View view) {
|
||||||
|
callApi(new BooleanSupplier() {
|
||||||
|
@Override
|
||||||
|
public boolean getAsBoolean() {
|
||||||
mShortcutManager.deleteAllDynamicShortcuts();
|
mShortcutManager.deleteAllDynamicShortcuts();
|
||||||
refreshList();
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static String formatTime(long time) {
|
||||||
|
Time tobj = new Time();
|
||||||
|
tobj.set(time);
|
||||||
|
return tobj.format("%Y-%m-%d %H:%M:%S");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onAddPressed(View view) {
|
||||||
|
final ShortcutInfo si = addRandomIntents(new ShortcutInfo.Builder(this)
|
||||||
|
.setId("shortcut-" + formatTime(System.currentTimeMillis()))
|
||||||
|
.setWeight(10)).build();
|
||||||
|
callApi(new BooleanSupplier() {
|
||||||
|
@Override
|
||||||
|
public boolean getAsBoolean() {
|
||||||
|
return mShortcutManager.addDynamicShortcut(si);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onUpdatePressed(View view) {
|
||||||
|
final List updateList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (ShortcutInfo si : getAllShortcuts()) {
|
||||||
|
updateList.add(addRandomIntents(new ShortcutInfo.Builder(this)
|
||||||
|
.setId(si.getId()))
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
callApi(new BooleanSupplier() {
|
||||||
|
@Override
|
||||||
|
public boolean getAsBoolean() {
|
||||||
|
return mShortcutManager.updateShortcuts(updateList);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void launch(ShortcutInfo si) {
|
void launch(ShortcutInfo si) {
|
||||||
@@ -231,7 +304,7 @@ public class ShortcutPublisher extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getAction2Text(ShortcutInfo si) {
|
protected String getAction2Text(ShortcutInfo si) {
|
||||||
return "Delete Dynamic";
|
return "Delete";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user