导航菜单

页面标题

页面副标题

i.BarIS v2024.401.30.180 - ShortcutsPlugin.java 源代码

正在查看: i.BarIS v2024.401.30.180 应用的 ShortcutsPlugin.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.plugins.shortcuts;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
import com.google.firebase.messaging.Constants;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShortcutsPlugin extends CordovaPlugin {
    private static final String ACTION_ADD_PINNED = "addPinned";
    private static final String ACTION_CHECK_DYNAMIC = "checkDynamic";
    private static final String ACTION_GET_INTENT = "getIntent";
    private static final String ACTION_ON_NEW_INTENT = "onNewIntent";
    private static final String ACTION_SET_DYNAMIC = "setDynamic";
    private static final String ACTION_SUPPORTS_DYNAMIC = "supportsDynamic";
    private static final String ACTION_SUPPORTS_PINNED = "supportsPinned";
    private static final String TAG = "ShortcutsPlugin";
    private CallbackContext onNewIntentCallbackContext = null;

    public boolean execute(String str, JSONArray jSONArray, CallbackContext callbackContext) {
        try {
            if (str.equals(ACTION_SUPPORTS_DYNAMIC)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, Build.VERSION.SDK_INT >= 25));
                return true;
            }
            if (str.equals(ACTION_SUPPORTS_PINNED)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, supportsPinned()));
                return true;
            }
            if (str.equals(ACTION_SET_DYNAMIC)) {
                setDynamicShortcuts(jSONArray);
                callbackContext.success();
                return true;
            }
            if (str.equals(ACTION_CHECK_DYNAMIC)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, checkDynamicShortcut(jSONArray)));
                return true;
            }
            if (str.equals(ACTION_ADD_PINNED)) {
                if (addPinnedShortcut(jSONArray)) {
                    callbackContext.success();
                } else {
                    callbackContext.error("Pinned shortcuts are not supported by the default launcher.");
                }
            } else {
                if (str.equals(ACTION_GET_INTENT)) {
                    getIntent(callbackContext);
                    return true;
                }
                if (str.equals(ACTION_ON_NEW_INTENT)) {
                    subscribeOnNewIntent(jSONArray, callbackContext);
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "Exception executing Cordova action: " + e.getMessage());
            callbackContext.error(e.getMessage());
            return true;
        }
    }

    public void onNewIntent(Intent intent) {
        try {
            if (this.onNewIntentCallbackContext != null) {
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, buildIntent(intent));
                pluginResult.setKeepCallback(true);
                this.onNewIntentCallbackContext.sendPluginResult(pluginResult);
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception handling onNewIntent: " + e.getMessage());
        }
    }

    private boolean supportsPinned() {
        return ShortcutManagerCompat.isRequestPinShortcutSupported(this.cordova.getActivity().getApplicationContext());
    }

    private void subscribeOnNewIntent(JSONArray jSONArray, CallbackContext callbackContext) throws JSONException {
        if (jSONArray.optBoolean(0)) {
            this.onNewIntentCallbackContext = null;
            Log.i(TAG, "Removed callback for onNewIntent");
        } else {
            this.onNewIntentCallbackContext = callbackContext;
            Log.i(TAG, "Added a new callback for onNewIntent");
        }
    }

    private void getIntent(CallbackContext callbackContext) throws JSONException {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, buildIntent(this.cordova.getActivity().getIntent())));
    }

    private JSONObject buildIntent(Intent intent) throws JSONException {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("action", intent.getAction());
        jSONObject.put("flags", intent.getFlags());
        Set<String> categories = intent.getCategories();
        if (categories != null) {
            jSONObject.put("categories", new JSONArray((Collection) categories));
        }
        Uri data = intent.getData();
        if (data != null) {
            jSONObject.put(Constants.ScionAnalytics.MessageType.DATA_MESSAGE, data.toString());
        }
        Bundle extras = intent.getExtras();
        if (extras != null) {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject.put("extras", jSONObject2);
            for (String str : extras.keySet()) {
                Object obj = extras.get(str);
                if (obj instanceof Boolean) {
                    jSONObject2.put(str, (Boolean) obj);
                } else if (obj instanceof Integer) {
                    jSONObject2.put(str, (Integer) obj);
                } else if (obj instanceof Long) {
                    jSONObject2.put(str, (Long) obj);
                } else if (obj instanceof Float) {
                    jSONObject2.put(str, (Float) obj);
                } else if (obj instanceof Double) {
                    jSONObject2.put(str, (Double) obj);
                } else {
                    jSONObject2.put(str, obj.toString());
                }
            }
        }
        return jSONObject;
    }

    private Intent parseIntent(JSONObject jSONObject) throws JSONException {
        Intent intent = new Intent();
        String optString = jSONObject.optString("activityClass", this.cordova.getActivity().getClass().getName());
        String optString2 = jSONObject.optString("activityPackage", this.cordova.getActivity().getPackageName());
        intent.setClassName(optString2, optString);
        String optString3 = jSONObject.optString("action", "android.intent.action.VIEW");
        if (optString3.indexOf(46) < 0) {
            optString3 = optString2 + '.' + optString3;
        }
        Log.i(TAG, "Creating new intent with action: " + optString3);
        intent.setAction(optString3);
        intent.setFlags(jSONObject.optInt("flags", 335544320));
        JSONArray optJSONArray = jSONObject.optJSONArray("categories");
        if (optJSONArray != null) {
            int length = optJSONArray.length();
            for (int i = 0; i < length; i++) {
                String string = optJSONArray.getString(i);
                if (string.indexOf(46) < 0) {
                    string = optString2 + '.' + string;
                }
                intent.addCategory(string);
            }
        }
        String optString4 = jSONObject.optString(Constants.ScionAnalytics.MessageType.DATA_MESSAGE);
        if (optString4.length() > 0) {
            intent.setData(Uri.parse(optString4));
        }
        JSONObject optJSONObject = jSONObject.optJSONObject("extras");
        if (optJSONObject != null) {
            Iterator<String> keys = optJSONObject.keys();
            while (keys.hasNext()) {
                String next = keys.next();
                Object obj = optJSONObject.get(next);
                if (obj != null) {
                    if (next.indexOf(46) < 0) {
                        next = optString2 + "." + next;
                    }
                    if (obj instanceof Boolean) {
                        intent.putExtra(next, (Boolean) obj);
                    } else if (obj instanceof Integer) {
                        intent.putExtra(next, (Integer) obj);
                    } else if (obj instanceof Long) {
                        intent.putExtra(next, (Long) obj);
                    } else if (obj instanceof Float) {
                        intent.putExtra(next, (Float) obj);
                    } else if (obj instanceof Double) {
                        intent.putExtra(next, (Double) obj);
                    } else {
                        intent.putExtra(next, obj.toString());
                    }
                }
            }
        }
        return intent;
    }

    private ShortcutInfo buildDynamicShortcut(JSONObject jSONObject) throws PackageManager.NameNotFoundException, JSONException {
        Icon createWithResource;
        if (jSONObject == null) {
            throw new InvalidParameterException("Shortcut object cannot be null");
        }
        Context applicationContext = this.cordova.getActivity().getApplicationContext();
        String optString = jSONObject.optString("id");
        if (optString.length() == 0) {
            throw new InvalidParameterException("A value for 'id' is required");
        }
        ShortcutInfo.Builder builder = new ShortcutInfo.Builder(applicationContext, optString);
        String optString2 = jSONObject.optString("shortLabel");
        String optString3 = jSONObject.optString("longLabel");
        if (optString2.length() == 0 && optString3.length() == 0) {
            throw new InvalidParameterException("A value for either 'shortLabel' or 'longLabel' is required");
        }
        if (optString2.length() == 0) {
            optString2 = optString3;
        }
        if (optString3.length() == 0) {
            optString3 = optString2;
        }
        String optString4 = jSONObject.optString("iconBitmap");
        String optString5 = jSONObject.optString("iconFromResource");
        String packageName = this.cordova.getActivity().getPackageName();
        if (optString4.length() > 0) {
            Icon.createWithBitmap(decodeBase64Bitmap(optString4));
        }
        if (optString5.length() > 0) {
            createWithResource = Icon.createWithResource(applicationContext, this.cordova.getActivity().getResources().getIdentifier(optString5, "drawable", packageName));
        } else {
            createWithResource = Icon.createWithResource(packageName, applicationContext.getPackageManager().getApplicationInfo(packageName, 128).icon);
        }
        JSONObject optJSONObject = jSONObject.optJSONObject("intent");
        if (optJSONObject == null) {
            optJSONObject = new JSONObject();
        }
        return builder.setShortLabel(optString2).setLongLabel(optString3).setIntent(parseIntent(optJSONObject)).setIcon(createWithResource).build();
    }

    private void setDynamicShortcuts(JSONArray jSONArray) throws PackageManager.NameNotFoundException, JSONException {
        int length = jSONArray.length();
        ArrayList arrayList = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            arrayList.add(buildDynamicShortcut(jSONArray.optJSONObject(i)));
        }
        ((ShortcutManager) this.cordova.getActivity().getApplicationContext().getSystemService(ShortcutManager.class)).setDynamicShortcuts(arrayList);
        Log.i(TAG, String.format("Saved % dynamic shortcuts.", Integer.valueOf(length)));
    }

    private boolean checkDynamicShortcut(JSONArray jSONArray) {
        List<ShortcutInfo> pinnedShortcuts;
        String optString = jSONArray.optJSONObject(0).optString("name");
        ShortcutManager shortcutManager = (ShortcutManager) this.cordova.getActivity().getApplicationContext().getSystemService(ShortcutManager.class);
        if (shortcutManager.getDynamicShortcuts().size() == 0 && shortcutManager.getPinnedShortcuts().size() > 0 && (pinnedShortcuts = shortcutManager.getPinnedShortcuts()) != null) {
            Iterator<ShortcutInfo> it = pinnedShortcuts.iterator();
            while (it.hasNext()) {
                if (it.next().getId().equals(optString)) {
                    return true;
                }
            }
        }
        return false;
    }

    private ShortcutInfoCompat buildPinnedShortcut(JSONObject jSONObject) throws PackageManager.NameNotFoundException, JSONException {
        IconCompat createWithResource;
        if (jSONObject == null) {
            throw new InvalidParameterException("Parameters must include a valid shorcut.");
        }
        Context applicationContext = this.cordova.getActivity().getApplicationContext();
        String optString = jSONObject.optString("id");
        if (optString.length() == 0) {
            throw new InvalidParameterException("A value for 'id' is required");
        }
        ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(applicationContext, optString);
        String optString2 = jSONObject.optString("shortLabel");
        String optString3 = jSONObject.optString("longLabel");
        if (optString2.length() == 0 && optString3.length() == 0) {
            throw new InvalidParameterException("A value for either 'shortLabel' or 'longLabel' is required");
        }
        if (optString2.length() == 0) {
            optString2 = optString3;
        }
        if (optString3.length() == 0) {
            optString3 = optString2;
        }
        String optString4 = jSONObject.optString("iconBitmap");
        if (optString4.length() > 0) {
            createWithResource = IconCompat.createWithBitmap(decodeBase64Bitmap(optString4));
        } else {
            createWithResource = IconCompat.createWithResource(applicationContext, applicationContext.getPackageManager().getApplicationInfo(this.cordova.getActivity().getPackageName(), 128).icon);
        }
        JSONObject optJSONObject = jSONObject.optJSONObject("intent");
        if (optJSONObject == null) {
            optJSONObject = new JSONObject();
        }
        Intent parseIntent = parseIntent(optJSONObject);
        return builder.setActivity(parseIntent.getComponent()).setShortLabel(optString2).setLongLabel(optString3).setIcon(createWithResource).setIntent(parseIntent).build();
    }

    private boolean addPinnedShortcut(JSONArray jSONArray) throws PackageManager.NameNotFoundException, JSONException {
        return ShortcutManagerCompat.requestPinShortcut(this.cordova.getActivity().getApplicationContext(), buildPinnedShortcut(jSONArray.optJSONObject(0)), null);
    }

    private static Bitmap decodeBase64Bitmap(String str) {
        byte[] decode = Base64.decode(str, 0);
        return BitmapFactory.decodeByteArray(decode, 0, decode.length);
    }
}