Fix demo launcher am: 74d5f56ed8
am: ad36445b18
Change-Id: I36b7f93212ef4599745ad9098e5daafd35db5057
This commit is contained in:
@@ -15,17 +15,14 @@
|
||||
*/
|
||||
package com.example.android.pm.shortcutlauncherdemo;
|
||||
|
||||
import android.app.ListFragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.LauncherActivityInfo;
|
||||
import android.content.pm.LauncherApps;
|
||||
import android.content.pm.LauncherApps.ShortcutQuery;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -36,7 +33,6 @@ import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -159,6 +155,7 @@ public class AppListFragment extends MyBaseListFragment {
|
||||
v.setTag(ai);
|
||||
|
||||
v.setVisibility(View.INVISIBLE);
|
||||
try {
|
||||
if (mLauncherApps.hasShortcutHostPermission()) {
|
||||
mQuery.setPackage(ai.getComponentName().getPackageName());
|
||||
mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC
|
||||
@@ -173,6 +170,9 @@ public class AppListFragment extends MyBaseListFragment {
|
||||
v.setText("Shortcuts");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.w(Global.TAG, "Caught exception", e);
|
||||
}
|
||||
}
|
||||
|
||||
final TextView line1 = (TextView) view.findViewById(R.id.line1);
|
||||
@@ -191,8 +191,12 @@ public class AppListFragment extends MyBaseListFragment {
|
||||
final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag();
|
||||
switch (v.getId()) {
|
||||
case R.id.launch:
|
||||
try {
|
||||
mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(),
|
||||
null, null);
|
||||
} catch (Exception e) {
|
||||
Global.showToast(getContext(), e.getMessage());
|
||||
}
|
||||
return;
|
||||
case R.id.action2:
|
||||
showShortcutsForPackage(ai);
|
||||
|
||||
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package com.example.android.pm.shortcutlauncherdemo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class Global {
|
||||
public static final String TAG = "ShortcutLauncherDemo";
|
||||
|
||||
public static void showToast(Context context, String message) {
|
||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import android.support.v4.view.ViewPager;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class ShortcutLauncherMain extends Activity {
|
||||
private LauncherApps mLauncherApps;
|
||||
private ViewPager mPager;
|
||||
private PagerAdapter mPagerAdapter;
|
||||
|
||||
@@ -40,8 +39,6 @@ public class ShortcutLauncherMain extends Activity {
|
||||
|
||||
setContentView(R.layout.main);
|
||||
|
||||
mLauncherApps = getSystemService(LauncherApps.class);
|
||||
|
||||
mPager = (ViewPager) findViewById(R.id.pager);
|
||||
mPager.setOffscreenPageLimit(2);
|
||||
mPagerAdapter = new MyPagerAdapter(getFragmentManager());
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.android.pm.shortcutdemo.ShortcutAdapter;
|
||||
@@ -93,29 +94,39 @@ public class ShortcutListFragment extends MyBaseListFragment {
|
||||
private void togglePin(ShortcutInfo selected) {
|
||||
final String packageName = selected.getPackage();
|
||||
|
||||
try {
|
||||
final ShortcutQuery q = new ShortcutQuery()
|
||||
.setPackage(packageName)
|
||||
.setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
|
||||
;
|
||||
|
||||
final List<String> pinned = new ArrayList<>();
|
||||
for (ShortcutInfo si : mAdapter.getShortcuts()) {
|
||||
if (si.isPinned()
|
||||
&& si.getPackage().equals(packageName)
|
||||
&& si.getUserHandle().equals(selected.getUserHandle())) {
|
||||
for (ShortcutInfo si : mLauncherApps.getShortcuts(q, android.os.Process.myUserHandle())) {
|
||||
pinned.add(si.getId());
|
||||
}
|
||||
}
|
||||
if (selected.isPinned()) {
|
||||
pinned.remove(selected.getId());
|
||||
} else {
|
||||
pinned.add(selected.getId());
|
||||
}
|
||||
mLauncherApps.pinShortcuts(packageName, pinned, selected.getUserHandle());
|
||||
} catch (Exception e) {
|
||||
Global.showToast(getContext(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void launch(ShortcutInfo si) {
|
||||
try {
|
||||
mLauncherApps.startShortcut(si.getPackage(), si.getId(), null, null,
|
||||
si.getUserHandle());
|
||||
} catch (Exception e) {
|
||||
Global.showToast(getContext(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshList() {
|
||||
try {
|
||||
if (!mLauncherApps.hasShortcutHostPermission()) {
|
||||
return;
|
||||
}
|
||||
@@ -136,6 +147,9 @@ public class ShortcutListFragment extends MyBaseListFragment {
|
||||
Collections.sort(list, mShortcutComparator);
|
||||
|
||||
mAdapter.setShortcuts(list);
|
||||
} catch (Exception e) {
|
||||
Log.w(Global.TAG, "Caught exception", e);
|
||||
}
|
||||
}
|
||||
|
||||
private final Comparator<ShortcutInfo> mShortcutComparator =
|
||||
|
||||
Reference in New Issue
Block a user