Bladeren bron

首先使用support v4 ShortcutManagerCompat来添加快捷入口

zengjiebin 7 jaren geleden
bovenliggende
commit
4a5ef1ac26

+ 4 - 2
app/src/main/AndroidManifest.xml

@@ -446,7 +446,8 @@
             android:screenOrientation="portrait"
             android:theme="@style/AppActionTheme" >
             <intent-filter>
-                <action android:name="android.intent.action.CREATE_SHORTCUT" />
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
         <activity
@@ -454,7 +455,8 @@
             android:screenOrientation="portrait"
             android:theme="@style/AppActionTheme" >
             <intent-filter>
-                <action android:name="android.intent.action.CREATE_SHORTCUT" />
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
         <activity

+ 33 - 7
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -23,7 +23,9 @@ import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.os.Build;
 import android.provider.AlarmClock;
-import android.provider.Settings;
+import android.support.v4.content.pm.ShortcutInfoCompat;
+import android.support.v4.content.pm.ShortcutManagerCompat;
+import android.support.v4.graphics.drawable.IconCompat;
 import android.text.TextUtils;
 import android.text.format.Formatter;
 import android.util.Log;
@@ -31,6 +33,7 @@ import android.view.Gravity;
 import android.view.View;
 import android.widget.TextView;
 
+import com.alibaba.fastjson.JSON;
 import com.allen.android.lib.rom.HuaweiUtils;
 import com.allen.android.lib.rom.MeizuUtils;
 import com.allen.android.lib.rom.MiuiUtils;
@@ -47,7 +50,6 @@ import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.Version;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.view.activity.ActWebX5YF;
-import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.Config;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
@@ -838,11 +840,29 @@ public class SysAppUtil {
         }
     }
 
+    //使用自定义图标创建快捷方式
+    public static boolean installShortCut(Context context) {
+        if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
+            IconCompat iconCompat = IconCompat.createWithResource(context, R.drawable.icon_yf);
+            Intent intent = getShortCutIntent(context);
+            intent.setAction(Intent.ACTION_MAIN);
+            String name = Config.YF_SHOP_NAME;//context.getString(R.string.app_name_yf);
+            ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(context, "9527").setShortLabel(name).setLongLabel(name).setIntent(intent).setIcon(iconCompat).build();
+            Intent pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(context, pinShortcutInfo);
 
+            PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, 0);
+
+            ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, successCallback.getIntentSender());
+            return true;
+        }
+        return false;
+    }
 
     //添加有范商城快捷入口
     public static void addShortcutTest(Activity activity) {
-        if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.O){
+        if(installShortCut(activity)){
+
+        } else if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.O){
             SysAppUtil.addShortcutTest8(activity);
         } else if(Build.VERSION.SDK_INT >=  Build.VERSION_CODES.N_MR1){
             SysAppUtil.addShortcutTest7(activity);
@@ -868,7 +888,7 @@ public class SysAppUtil {
 
             ShortcutInfo info = new ShortcutInfo.Builder(activity, String.valueOf(mShortcutName.hashCode()))
                     .setIcon(Icon.createWithResource(activity, R.drawable.icon_yf))
-                    .setShortLabel(mShortcutName.substring(0,2))
+                    .setShortLabel(mShortcutName)
                     .setLongLabel(mShortcutName)
                     .setIntent(shortcutInfoIntent)
                     .build();
@@ -928,7 +948,7 @@ public class SysAppUtil {
         //设置ID
         ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(activity, String.valueOf(mShortcutName.hashCode()))
                 //设置短标题
-                .setShortLabel(mShortcutName.substring(0,2))
+                .setShortLabel(mShortcutName)
                 //设置长标题
                 .setLongLabel(mShortcutName)
                 //设置icon
@@ -959,11 +979,11 @@ public class SysAppUtil {
         // ShortcutSuperUtils.addShortcutByPackageName(this, this.getPackageName());
     }
     public static final Class<? extends Activity> yfCls = ActWebX5YF.class;
-    private static Intent getShortCutIntent(Activity activity) {
+    private static Intent getShortCutIntent(Context context) {
         // 使用MAIN,可以避免部分手机(比如华为、HTC部分机型)删除应用时无法删除快捷方式的问题
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_DEFAULT);
-        intent.setClass(activity, yfCls);
+        intent.setClass(context, yfCls);
         return intent;
     }
 
@@ -981,6 +1001,10 @@ public class SysAppUtil {
         long end = System.currentTimeMillis() - start;
         Log.e("Finals", end + "  eee");
         String url = "content://" + authority + "/favorites?notify=true";
+        return hasShortcut(context, appName, url);
+    }
+    //是否有快捷方式的权限
+    public static boolean hasShortcut(Context context, String appName, String url) {
         Cursor cursor = null;
         try {
             Uri CONTENT_URI = Uri.parse(url);
@@ -1011,12 +1035,14 @@ public class SysAppUtil {
         }
         List<ProviderInfo> info = mPackageManager.queryContentProviders(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.applicationInfo.uid, PackageManager.GET_PROVIDERS);
         if (info != null) {
+            LogUtil.println("getAuthorityFromPermission", JSON.toJSONString(info));
             for (int j = 0; j < info.size(); j++) {
                 ProviderInfo provider = info.get(j);
                 if (provider.readPermission == null) {
                     continue;
                 }
                 if (Pattern.matches(".*launcher.*READ_SETTINGS", provider.readPermission)) {
+                    LogUtil.println("getAuthorityFromPermission", j, provider.authority);
                     return provider.authority;
                 }
             }

+ 34 - 1
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -18,6 +18,7 @@ import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationManagerCompat;
+import android.support.v4.content.pm.ShortcutManagerCompat;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
 import android.util.Log;
@@ -264,7 +265,7 @@ public class TestUtil {
      */
     public static void test(final Activity activity) {
         final String[] items = {"复制token", "添加token", "复制打点数据", "从jenkins下载小绵羊安装包", "测试表情包",
-                "测试跳转","判断是否添加商城快捷入口",
+                "测试跳转","support包尝试创建桌面快捷方式","判断Launcher是否支持创建桌面快捷方式","判断是否添加快捷入口","判断是否添加商城快捷入口",
                 "跳转QQ", "跳转QQ群", "尝试获取通讯录", "测试有范","测试有范6.0","测试有范7.0","测试有范8.0", "测试设备信息", "下载视频测试", "爱奇艺VIP会员", "分享我的签名的小绵羊apk", "分享当前小绵羊apk",
                 "测试升级对话框", "少数民族姓名", "开启皮肤", "不开启皮肤", "我的游戏", "优惠券弹窗", "填写收件地址弹窗",
                 "朗读文字", "游戏搜索", "测试插件", "测试bitmap", "剪切视频", "我的关注", "足迹",
@@ -293,6 +294,16 @@ public class TestUtil {
             case "测试跳转":
                 testSkip(activity);
                 break;
+            case "support包尝试创建桌面快捷方式":
+                SysAppUtil.installShortCut(activity);
+                break;
+            case "判断Launcher是否支持创建桌面快捷方式":
+                boolean isRequestPinShortcutSupported = ShortcutManagerCompat.isRequestPinShortcutSupported(activity);
+                G.showToast((isRequestPinShortcutSupported ? "" : "不") +"支持");
+                break;
+            case "判断是否添加快捷入口":
+                testHasShortCut();
+                break;
             case "判断是否添加商城快捷入口":
                 boolean hasShortcut = SysAppUtil.hasShortcut(SheepApp.getInstance(), Config.YF_SHOP_NAME);
                 G.showToast((hasShortcut ? "已经": "还未") + Config.YF_SHOP_NAME + "桌面快捷入口");
@@ -728,6 +739,28 @@ public class TestUtil {
         }
     }
 
+    //通过粘贴板内容测试是否有快捷入口
+    private static void testHasShortCut() {
+        CharSequence charSequence = StringUtils.getCopyText();
+        if(charSequence == null){
+            G.showToast("请在粘贴板中复制如下内容:有范商城;content://com.android.launcher.settings/favorites?notify=true");
+            return;
+        }
+        String copyText = charSequence.toString();
+        String name;
+        String url;
+        if(copyText.contains(";")){
+            String[] items = copyText.split(";");
+            name = items[0];
+            url = items[1];
+        } else {
+            name = "有范商城";
+            url = copyText;
+        }
+        boolean hasShortcut1 = SysAppUtil.hasShortcut(SheepApp.getInstance(), name, url);
+        G.showToast((hasShortcut1 ? "已经": "还未") + name + "桌面快捷入口");
+    }
+
     private static void testSkip(Activity activity) {
         CharSequence charSequence = StringUtils.getCopyText();
         if(charSequence == null){

+ 5 - 5
app/src/main/java/com/sheep/gamegroup/view/activity/ActWebX5.java

@@ -130,15 +130,15 @@ public class ActWebX5 extends BaseActWeb {
     private void checkWebParams(WebParams webParams) {
         if(webParams.getUrl().startsWith(Config.YF_SHOP_HOME_SHEEP.split("\\?")[0]) || webParams.getUrl().startsWith(Config.YF_SHOP_HOME_SHEEP_TEST.split("\\?")[0])) {//检测是否是有范商城地址
             boolean isIgnore = SpUtils.getAsBoolean(KEY_IGNORE_ADD_YOU_FAN_LAUNCHER, false);
-//            boolean hasShortcut = SysAppUtil.hasShortcut(SheepApp.getInstance(), Config.YF_SHOP_NAME);
-//            boolean isShow = !hasShortcut && !isIgnore && !CommonUtil.getInstance().hasYouFanLauncher();//未忽略并且没有添加有范商品入口
-            boolean isShow = !isIgnore && !SysAppUtil.isEnableComponentYf();
+            boolean hasShortcut = SysAppUtil.hasShortcut(SheepApp.getInstance(), Config.YF_SHOP_NAME);
+            boolean isShow = !hasShortcut && !isIgnore && !CommonUtil.getInstance().hasYouFanLauncher();//未忽略并且没有添加有范商品入口
+//            boolean isShow = !isIgnore && !SysAppUtil.isEnableComponentYf();
             if (isShow) {
                 ViewUtil.showMsgDialog(this, new DialogConfig().setTitle("温馨提示")
                         .setMsg("亲,喜欢的话,就添加一个商城的桌面快捷入口吧!(点击否将不再提示)")
                         .setBtnLeftText("否").setBtnLeftOnClickListener(view -> SpUtils.putAsBoolean(KEY_IGNORE_ADD_YOU_FAN_LAUNCHER, true))
-//                        .setBtnRightText("是").setBtnRightOnClickListener(view -> CommonUtil.getInstance().addYouFanLauncher()));
-                        .setBtnRightText("是").setBtnRightOnClickListener(view -> SysAppUtil.enableComponentYf()));
+                        .setBtnRightText("是").setBtnRightOnClickListener(view -> CommonUtil.getInstance().addYouFanLauncher()));
+//                        .setBtnRightText("是").setBtnRightOnClickListener(view -> SysAppUtil.enableComponentYf()));
             }
         }
     }