Skip to content

Commit

Permalink
修复在Android O上创建快捷方式失败的问题;替换默认快捷方式图标
Browse files Browse the repository at this point in the history
  • Loading branch information
hyb1996 committed Jul 7, 2018
1 parent dc468c7 commit e247943
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 30 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.application'
def AAVersion = '4.3.1'

android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "org.autojs.autojs"
minSdkVersion 17
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
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.
*/

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public class ShortcutManager {


private static ShortcutManager sInstance;
private Context mContext;
private android.content.pm.ShortcutManager mShortcutManager;
Expand All @@ -34,14 +43,35 @@ public static ShortcutManager getInstance(Context context) {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -222,6 +223,7 @@ public void createShortcut(ScriptFile file) {
.putExtra(ShortcutCreateActivity.EXTRA_FILE, file));
}

@SuppressLint("CheckResult")
public void delete(final ScriptFile scriptFile) {
Observable.fromPublisher(new Publisher<Boolean>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -86,9 +87,11 @@ void selectIcon() {
}


@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);
Expand All @@ -105,22 +108,29 @@ private void createShortcut() {
}

@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) {
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/drawable/ic_node_js_black.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="128dp"
android:height="128dp"
android:viewportWidth="128"
android:viewportHeight="128">
android:viewportHeight="128"
android:viewportWidth="128">

<path
android:fillColor="#46483E"
android:pathData="M64,14c-27.6,0-50,22.4-50,50s22.4,50,50,50s50-22.4,50-50S91.6,14,64,14z" />
android:pathData="M64,14c-27.6,0-50,22.4-50,50s22.4,50,50,50s50-22.4,50-50S91.6,14,64,14z"/>
<path
android:fillColor="#81C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8,3.2c7.2,0.9,10.6,2.3,10.6,7.3C82.9,74.4,78.7,77.3,71.3,77.3z" />
c0,1.7,0.7,2.2,8,3.2c7.2,0.9,10.6,2.3,10.6,7.3C82.9,74.4,78.7,77.3,71.3,77.3z"/>
</vector>
4 changes: 2 additions & 2 deletions autojs/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
compileSdkVersion 27
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,6 @@ public boolean clipPath(@NonNull Path path) {
return mCanvas.clipPath(path);
}

@Deprecated
public boolean clipRegion(Region region, Region.Op op) {
return mCanvas.clipRegion(region, op);
}

@Deprecated
public boolean clipRegion(Region region) {
return mCanvas.clipRegion(region);
}

public DrawFilter getDrawFilter() {
return mCanvas.getDrawFilter();
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e247943

Please sign in to comment.