Skip to content

Commit 7f5f4a7

Browse files
committed
1.创建快捷方式的时候如果没有获取到权限,提示用户
1 parent 5234fc5 commit 7f5f4a7

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

qpython/src/main/java/org/qpython/qpy/main/activity/AppListActivity.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.qpython.qpy.main.activity;
22

33
import android.Manifest;
4+
import android.app.ActivityManager;
45
import android.app.AlertDialog;
56
import android.app.LoaderManager;
67
import android.app.PendingIntent;
@@ -9,11 +10,13 @@
910
import android.content.IntentFilter;
1011
import android.content.Loader;
1112
import android.content.pm.PackageManager;
13+
import android.content.pm.ResolveInfo;
1214
import android.content.pm.ShortcutInfo;
1315
import android.content.pm.ShortcutManager;
1416
import android.graphics.drawable.Icon;
1517
import android.os.Build;
1618
import android.os.Bundle;
19+
import android.os.Handler;
1720
import android.support.annotation.NonNull;
1821
import android.support.annotation.Nullable;
1922
import android.support.v4.app.ActivityCompat;
@@ -38,6 +41,7 @@
3841
import org.qpython.qpy.main.event.AppsLoader;
3942
import org.qpython.qpy.main.model.AppModel;
4043
import org.qpython.qpy.main.model.QPyScriptModel;
44+
import org.qpython.qpy.utils.ShortcutUtil;
4145
import org.qpython.qpysdk.QPyConstants;
4246
import org.qpython.qpysdk.utils.Utils;
4347
import org.qpython.qsl4a.qsl4a.LogUtil;
@@ -145,6 +149,7 @@ public void createShortcut(QPyScriptModel item) {
145149
// return;
146150
// }
147151
createShortcutOnThis();
152+
// test();
148153
}
149154

150155
@Override
@@ -191,6 +196,10 @@ public void exit() {
191196
// }
192197
// }
193198

199+
private void test(){
200+
judgeShortcutNameV2("org.qpython.qpy");
201+
}
202+
194203
private void createShortcutOnThis(){
195204
if (mBean == null){
196205
return;
@@ -217,9 +226,17 @@ private void createShortcutOnThis(){
217226
mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
218227
PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
219228
pinnedShortcutCallbackIntent, 0);
220-
229+
LogUtil.e("createShortcut: " + "111111111111");
221230
mShortcutManager.requestPinShortcut(pinShortcutInfo,
222231
successCallback.getIntentSender());
232+
LogUtil.e("createShortcut: " + mBean.getLabel());
233+
new Handler().postDelayed(new Runnable() {
234+
@Override
235+
public void run() {
236+
judgeShortcutNameV2("org.qpython.qpy");
237+
// judgeShortcutName(mBean.getLabel());
238+
}
239+
},200);
223240
}
224241
} else {
225242
//Adding shortcut for MainActivity
@@ -236,6 +253,24 @@ private void createShortcutOnThis(){
236253
}
237254
}
238255

256+
// private void judgeShortcutName(String name) {
257+
// int shortcutNum = 0;
258+
// for (String packageName : ShortcutUtil.getAllTheLauncher(getApplicationContext())) {
259+
// LogUtil.e("packageName111111: " + packageName);
260+
// if (name.equals(packageName)) {
261+
// shortcutNum ++;
262+
// }
263+
// }
264+
// LogUtil.e("packageName222222: " + shortcutNum);
265+
// }
266+
267+
private void judgeShortcutNameV2(String name) {
268+
if (!ShortcutUtil.getShortcutInfo(getApplicationContext()).isEmpty()){
269+
return;
270+
}
271+
Toast.makeText(this, getString(R.string.shortcut_create_fail), Toast.LENGTH_SHORT).show();
272+
}
273+
239274

240275
private void getScriptList() {
241276
try {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.qpython.qpy.utils;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.content.pm.PackageInfo;
6+
import android.content.pm.PackageManager;
7+
import android.content.pm.ResolveInfo;
8+
import android.content.pm.ShortcutInfo;
9+
import android.content.pm.ShortcutManager;
10+
import android.os.Build;
11+
import android.support.annotation.RequiresApi;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class ShortcutUtil {
17+
18+
public static List<String> getAllTheLauncher(Context context) {
19+
List<String> names = new ArrayList<>();
20+
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0);
21+
for (int i = 0; i < packs.size(); i++) {
22+
PackageInfo p = packs.get(i);
23+
24+
25+
if (p.versionName == null) {
26+
continue;
27+
}
28+
names.add(p.packageName);
29+
// newInfo.appname = p.applicationInfo.loadLabel(context.getPackageManager()).toString();
30+
// newInfo.pname = p.packageName;
31+
// newInfo.classname = p.applicationInfo.className;
32+
// newInfo.versionCode = p.versionCode;
33+
// newInfo.icon = p.applicationInfo.loadIcon(context.getPackageManager());
34+
// List<String> names = null;
35+
// PackageManager pkgMgt = context.getPackageManager();
36+
// Intent it = new Intent(Intent.ACTION_MAIN);
37+
// it.addCategory(Intent.CATEGORY_LAUNCHER);
38+
// List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
39+
// if (ra.size() != 0) {
40+
// names = new ArrayList<String>();
41+
// }
42+
// for (int i = 0; i < ra.size(); i++) {
43+
// String packageName = ra.get(i).activityInfo.packageName;
44+
// names.add(packageName);
45+
// }
46+
}
47+
return names;
48+
}
49+
50+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
51+
public static List<ShortcutInfo> getShortcutInfo(Context context){
52+
ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
53+
return mShortcutManager.getPinnedShortcuts();
54+
}
55+
}

qpython/src/main/res/values-ja/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,5 @@
489489
<string name="enable_keep_alive">You can run QPython script even it\'s in background if this mode is enabled.</string>
490490
<string name="disable_keep_alive">Disable</string>
491491
<string name="keep_alive_tips">QPython will restart to apply changes.</string>
492+
<string name="shortcut_create_fail">"Please grant the creat shortcut permission. "</string>
492493
</resources>

qpython/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,5 @@ w <string name="confirm">Подтвердить</string>
433433
<string name="enable_keep_alive">You can run QPython script even it\'s in background if this mode is enabled.</string>
434434
<string name="disable_keep_alive">Disable</string>
435435
<string name="keep_alive_tips">QPython will restart to apply changes.</string>
436+
<string name="shortcut_create_fail">"Please grant the creat shortcut permission. "</string>
436437
</resources>

qpython/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,5 +453,6 @@
453453
<string name="enable_keep_alive">You can run QPython script even it\'s in background if this mode is enabled.</string>
454454
<string name="disable_keep_alive">Disable</string>
455455
<string name="keep_alive_tips">QPython will restart to apply changes.</string>
456+
<string name="shortcut_create_fail">"Please grant the creat shortcut permission. "</string>
456457

457458
</resources>

qpython/src/main/res/values-zh-rTW/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,6 @@
294294
<string name="enable_keep_alive">You can run QPython script even it\'s in background if this mode is enabled.</string>
295295
<string name="disable_keep_alive">Disable</string>
296296
<string name="keep_alive_tips">QPython will restart to apply changes.</string>
297+
<string name="shortcut_create_fail">"Please grant the creat shortcut permission. "</string>
297298

298299
</resources>

qpython/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,5 +739,6 @@
739739
<string name="disable_keep_alive">Disable</string>
740740
<string name="keep_alive_mode">Keep Alive Mode</string>
741741
<string name="keep_alive_tips">QPython will restart to apply changes.</string>
742+
<string name="shortcut_create_fail">"Please grant the creat shortcut permission. "</string>
742743

743744
</resources>

0 commit comments

Comments
 (0)