Fix demo launcher am: 74d5f56ed8

am: ad36445b18

Change-Id: I36b7f93212ef4599745ad9098e5daafd35db5057
This commit is contained in:
Makoto Onuki
2016-07-29 20:52:21 +00:00
committed by android-build-merger
4 changed files with 74 additions and 52 deletions

View File

@@ -15,17 +15,14 @@
*/ */
package com.example.android.pm.shortcutlauncherdemo; package com.example.android.pm.shortcutlauncherdemo;
import android.app.ListFragment;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps; import android.content.pm.LauncherApps;
import android.content.pm.LauncherApps.ShortcutQuery; import android.content.pm.LauncherApps.ShortcutQuery;
import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@@ -36,7 +33,6 @@ import android.widget.BaseAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -159,6 +155,7 @@ public class AppListFragment extends MyBaseListFragment {
v.setTag(ai); v.setTag(ai);
v.setVisibility(View.INVISIBLE); v.setVisibility(View.INVISIBLE);
try {
if (mLauncherApps.hasShortcutHostPermission()) { if (mLauncherApps.hasShortcutHostPermission()) {
mQuery.setPackage(ai.getComponentName().getPackageName()); mQuery.setPackage(ai.getComponentName().getPackageName());
mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC
@@ -173,6 +170,9 @@ public class AppListFragment extends MyBaseListFragment {
v.setText("Shortcuts"); v.setText("Shortcuts");
} }
} }
} catch (Exception e) {
Log.w(Global.TAG, "Caught exception", e);
}
} }
final TextView line1 = (TextView) view.findViewById(R.id.line1); final TextView line1 = (TextView) view.findViewById(R.id.line1);
@@ -191,8 +191,12 @@ public class AppListFragment extends MyBaseListFragment {
final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag(); final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag();
switch (v.getId()) { switch (v.getId()) {
case R.id.launch: case R.id.launch:
try {
mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(), mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(),
null, null); null, null);
} catch (Exception e) {
Global.showToast(getContext(), e.getMessage());
}
return; return;
case R.id.action2: case R.id.action2:
showShortcutsForPackage(ai); showShortcutsForPackage(ai);

View File

@@ -15,6 +15,13 @@
*/ */
package com.example.android.pm.shortcutlauncherdemo; package com.example.android.pm.shortcutlauncherdemo;
import android.content.Context;
import android.widget.Toast;
public class Global { public class Global {
public static final String TAG = "ShortcutLauncherDemo"; public static final String TAG = "ShortcutLauncherDemo";
public static void showToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
} }

View File

@@ -30,7 +30,6 @@ import android.support.v4.view.ViewPager;
import android.view.ViewGroup; import android.view.ViewGroup;
public class ShortcutLauncherMain extends Activity { public class ShortcutLauncherMain extends Activity {
private LauncherApps mLauncherApps;
private ViewPager mPager; private ViewPager mPager;
private PagerAdapter mPagerAdapter; private PagerAdapter mPagerAdapter;
@@ -40,8 +39,6 @@ public class ShortcutLauncherMain extends Activity {
setContentView(R.layout.main); setContentView(R.layout.main);
mLauncherApps = getSystemService(LauncherApps.class);
mPager = (ViewPager) findViewById(R.id.pager); mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOffscreenPageLimit(2); mPager.setOffscreenPageLimit(2);
mPagerAdapter = new MyPagerAdapter(getFragmentManager()); mPagerAdapter = new MyPagerAdapter(getFragmentManager());

View File

@@ -24,6 +24,7 @@ import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.example.android.pm.shortcutdemo.ShortcutAdapter; import com.example.android.pm.shortcutdemo.ShortcutAdapter;
@@ -93,29 +94,39 @@ public class ShortcutListFragment extends MyBaseListFragment {
private void togglePin(ShortcutInfo selected) { private void togglePin(ShortcutInfo selected) {
final String packageName = selected.getPackage(); final String packageName = selected.getPackage();
try {
final ShortcutQuery q = new ShortcutQuery()
.setPackage(packageName)
.setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
;
final List<String> pinned = new ArrayList<>(); final List<String> pinned = new ArrayList<>();
for (ShortcutInfo si : mAdapter.getShortcuts()) { for (ShortcutInfo si : mLauncherApps.getShortcuts(q, android.os.Process.myUserHandle())) {
if (si.isPinned()
&& si.getPackage().equals(packageName)
&& si.getUserHandle().equals(selected.getUserHandle())) {
pinned.add(si.getId()); pinned.add(si.getId());
} }
}
if (selected.isPinned()) { if (selected.isPinned()) {
pinned.remove(selected.getId()); pinned.remove(selected.getId());
} else { } else {
pinned.add(selected.getId()); pinned.add(selected.getId());
} }
mLauncherApps.pinShortcuts(packageName, pinned, selected.getUserHandle()); mLauncherApps.pinShortcuts(packageName, pinned, selected.getUserHandle());
} catch (Exception e) {
Global.showToast(getContext(), e.getMessage());
}
} }
private void launch(ShortcutInfo si) { private void launch(ShortcutInfo si) {
try {
mLauncherApps.startShortcut(si.getPackage(), si.getId(), null, null, mLauncherApps.startShortcut(si.getPackage(), si.getId(), null, null,
si.getUserHandle()); si.getUserHandle());
} catch (Exception e) {
Global.showToast(getContext(), e.getMessage());
}
} }
@Override @Override
protected void refreshList() { protected void refreshList() {
try {
if (!mLauncherApps.hasShortcutHostPermission()) { if (!mLauncherApps.hasShortcutHostPermission()) {
return; return;
} }
@@ -136,6 +147,9 @@ public class ShortcutListFragment extends MyBaseListFragment {
Collections.sort(list, mShortcutComparator); Collections.sort(list, mShortcutComparator);
mAdapter.setShortcuts(list); mAdapter.setShortcuts(list);
} catch (Exception e) {
Log.w(Global.TAG, "Caught exception", e);
}
} }
private final Comparator<ShortcutInfo> mShortcutComparator = private final Comparator<ShortcutInfo> mShortcutComparator =