修复在Android O上创建快捷方式失败的问题;替换默认快捷方式图标
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
package org.autojs.autojs.external.shortcut;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.RequiresApi;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.subjects.PublishSubject;
|
||||
import io.reactivex.subjects.Subject;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/10/25.
|
||||
*/
|
||||
@@ -17,6 +25,7 @@ import java.util.List;
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
public class ShortcutManager {
|
||||
|
||||
|
||||
private static ShortcutManager sInstance;
|
||||
private Context mContext;
|
||||
private android.content.pm.ShortcutManager mShortcutManager;
|
||||
@@ -34,14 +43,35 @@ public class ShortcutManager {
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
public void addDynamicShortcut(CharSequence label, String id, Icon icon, Intent intent) {
|
||||
ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, id)
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public void addPinnedShortcut(CharSequence label, String id, Icon icon, Intent intent) {
|
||||
if (!mShortcutManager.isRequestPinShortcutSupported()) {
|
||||
return;
|
||||
}
|
||||
ShortcutInfo shortcut = buildShortcutInfo(label, id, icon, intent);
|
||||
int req = getRequestCode(id);
|
||||
PendingIntent successCallback = PendingIntent.getBroadcast(mContext, req,
|
||||
mShortcutManager.createShortcutResultIntent(shortcut), 0);
|
||||
mShortcutManager.requestPinShortcut(shortcut, successCallback.getIntentSender());
|
||||
}
|
||||
|
||||
private ShortcutInfo buildShortcutInfo(CharSequence label, String id, Icon icon, Intent intent) {
|
||||
return new ShortcutInfo.Builder(mContext, id)
|
||||
.setIntent(intent)
|
||||
.setShortLabel(label)
|
||||
.setLongLabel(label)
|
||||
.setIcon(icon)
|
||||
.build();
|
||||
}
|
||||
|
||||
private int getRequestCode(String id) {
|
||||
return id.hashCode() >>> 16;
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
public void addDynamicShortcut(CharSequence label, String id, Icon icon, Intent intent) {
|
||||
ShortcutInfo shortcut = buildShortcutInfo(label, id, icon, intent);
|
||||
try {
|
||||
addDynamicShortcutUnchecked(shortcut);
|
||||
} catch (IllegalArgumentException shortcutsExceeded) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.autojs.autojs.ui.common;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Environment;
|
||||
@@ -222,6 +223,7 @@ public class ScriptOperations {
|
||||
.putExtra(ShortcutCreateActivity.EXTRA_FILE, file));
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
public void delete(final ScriptFile scriptFile) {
|
||||
Observable.fromPublisher(new Publisher<Boolean>() {
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.autojs.autojs.ui.shortcut;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
@@ -86,9 +87,11 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("NewApi") //for fool android studio
|
||||
private void createShortcut() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && mUseAndroidNShortcut.isChecked()) {
|
||||
createShortcutForAndroidN();
|
||||
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && mUseAndroidNShortcut.isChecked())
|
||||
|| Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createShortcutByShortcutManager();
|
||||
return;
|
||||
}
|
||||
Shortcut shortcut = new Shortcut(this);
|
||||
@@ -105,22 +108,29 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
private void createShortcutForAndroidN() {
|
||||
private void createShortcutByShortcutManager() {
|
||||
Icon icon;
|
||||
if (mIsDefaultIcon) {
|
||||
icon = Icon.createWithResource(this, R.drawable.ic_node_js_black);
|
||||
icon = Icon.createWithResource(this, R.drawable.ic_file_type_js);
|
||||
} else {
|
||||
Bitmap bitmap = BitmapTool.drawableToBitmap(mIcon.getDrawable());
|
||||
icon = Icon.createWithBitmap(bitmap);
|
||||
}
|
||||
PersistableBundle extras = new PersistableBundle(1);
|
||||
extras.putString(ScriptIntents.EXTRA_KEY_PATH, mScriptFile.getPath());
|
||||
ShortcutManager.getInstance(this).addDynamicShortcut(mName.getText(), mScriptFile.getPath(), icon,
|
||||
new Intent(this, ShortcutActivity.class)
|
||||
.putExtra(ScriptIntents.EXTRA_KEY_PATH, mScriptFile.getPath())
|
||||
.setAction(Intent.ACTION_MAIN));
|
||||
Intent intent = new Intent(this, ShortcutActivity.class)
|
||||
.putExtra(ScriptIntents.EXTRA_KEY_PATH, mScriptFile.getPath())
|
||||
.setAction(Intent.ACTION_MAIN);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ShortcutManager.getInstance(this).addPinnedShortcut(mName.getText(), mScriptFile.getPath(), icon, intent);
|
||||
} else {
|
||||
ShortcutManager.getInstance(this).addDynamicShortcut(mName.getText(), mScriptFile.getPath(), icon, intent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (resultCode != RESULT_OK) {
|
||||
|
||||
Reference in New Issue
Block a user