Skip to content

Commit ff10bc6

Browse files
committed
1.keep alive 保活的通知栏图标更换为qpython的图标
2.setting中的keep alive item更改文案和增加重启的功能
1 parent 5c898af commit ff10bc6

File tree

11 files changed

+495
-322
lines changed

11 files changed

+495
-322
lines changed

qpython/src/main/AndroidManifest.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
<uses-permission android:name="android.permission.VIBRATE" />
2020
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
2121
<uses-permission android:name="com.android.vending.BILLING"/>
22-
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
23-
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2422
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2523
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2624
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />
@@ -35,6 +33,22 @@
3533
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
3634
<uses-permission android:name="net.dinglisch.android.tasker.PERMISSION_RUN_TASKS" />
3735

36+
<!--快捷方式开始-->
37+
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><!-- 添加快捷方式 -->
38+
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /><!-- 移除快捷方式 -->
39+
<uses-permission android:name="android.permission.INSTALL_SHORTCUT" />
40+
<uses-permission android:name="android.permission.UNINSTALL_SHORTCUT" />
41+
<!-- 查询快捷方式2.1以下 -->
42+
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
43+
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
44+
<!-- 查询快捷方式4.4及以下 -->
45+
<uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS" />
46+
<uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS" />
47+
<!-- 查询快捷方式4.4以上 -->
48+
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
49+
<uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
50+
<!--快捷方式结束-->
51+
3852
<uses-feature android:name="android.hardware.microphone" android:required="false"/>
3953
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
4054
<uses-feature android:name="android.hardware.camera" android:required="false"/>
@@ -68,6 +82,8 @@
6882
<service android:name=".main.service.QPyService"/>
6983
<service android:name="org.qpython.qsl4a.QPyScriptService"/>
7084
<service android:name=".main.service.FTPServerService"/>
85+
86+
7187
<activity
7288
android:name=".main.activity.SignInActivity"
7389
android:launchMode="singleTask"/>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.qpython.qpy.console.shortcuts;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
7+
import org.qpython.qsl4a.qsl4a.LogUtil;
8+
9+
public class ShortcutReceiver extends BroadcastReceiver {
10+
11+
12+
@Override
13+
public void onReceive(Context context, Intent intent) {
14+
LogUtil.e("111111111" + intent);
15+
}
16+
}

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

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package org.qpython.qpy.main.activity;
22

3-
import android.app.Activity;
3+
import android.Manifest;
44
import android.app.AlertDialog;
55
import android.app.LoaderManager;
6+
import android.app.PendingIntent;
67
import android.content.Context;
78
import android.content.Intent;
9+
import android.content.IntentFilter;
810
import android.content.Loader;
11+
import android.content.pm.PackageManager;
12+
import android.content.pm.ShortcutInfo;
13+
import android.content.pm.ShortcutManager;
14+
import android.graphics.drawable.Icon;
15+
import android.os.Build;
916
import android.os.Bundle;
17+
import android.support.annotation.NonNull;
1018
import android.support.annotation.Nullable;
11-
import android.support.v7.app.AppCompatActivity;
19+
import android.support.v4.app.ActivityCompat;
20+
import android.support.v4.content.ContextCompat;
1221
import android.support.v7.widget.GridLayoutManager;
1322
import android.support.v7.widget.LinearLayoutManager;
1423
import android.support.v7.widget.RecyclerView;
15-
import android.support.v7.widget.Toolbar;
16-
import android.view.Gravity;
17-
import android.widget.LinearLayout;
1824
import android.widget.TextView;
25+
import android.widget.Toast;
1926

2027
import com.quseit.util.FileHelper;
2128
import com.quseit.util.FolderUtils;
@@ -26,30 +33,37 @@
2633
import org.greenrobot.eventbus.ThreadMode;
2734
import org.qpython.qpy.R;
2835
import org.qpython.qpy.console.ScriptExec;
36+
import org.qpython.qpy.console.shortcuts.ShortcutReceiver;
2937
import org.qpython.qpy.main.adapter.AppListAdapter;
3038
import org.qpython.qpy.main.event.AppsLoader;
3139
import org.qpython.qpy.main.model.AppModel;
3240
import org.qpython.qpy.main.model.QPyScriptModel;
3341
import org.qpython.qpysdk.QPyConstants;
3442
import org.qpython.qpysdk.utils.Utils;
43+
import org.qpython.qsl4a.qsl4a.LogUtil;
3544

3645
import java.io.File;
3746
import java.io.IOException;
3847
import java.util.ArrayList;
3948
import java.util.Arrays;
4049
import java.util.List;
4150

51+
import static org.qpython.qpy.R2.string.show;
52+
4253
/**
4354
* Local App list
4455
* Created by Hmei on 2017-05-22.
4556
*/
4657

4758
public class AppListActivity extends BaseActivity implements LoaderManager.LoaderCallbacks<ArrayList<AppModel>> {
4859
public static final String TYPE_SCRIPT = "script";
60+
private static final int REQUEST_INSTALL_SHORTCUT = 0;
4961

5062
private List<AppModel> dataList;
5163
private AppListAdapter adapter;
5264

65+
ShortcutReceiver receiver;
66+
5367
public static void start(Context context, String type) {
5468
Intent starter = new Intent(context, AppListActivity.class);
5569
starter.putExtra("type", type);
@@ -59,12 +73,25 @@ public static void start(Context context, String type) {
5973
@Override
6074
protected void onCreate(@Nullable Bundle savedInstanceState) {
6175
super.onCreate(savedInstanceState);
76+
initShortcutReceiver();
6277
runShortcut();
6378
setContentView(R.layout.activity_local_app);
6479
initView();
6580
EventBus.getDefault().register(this);
6681
}
6782

83+
private void initShortcutReceiver() {
84+
IntentFilter filter = new IntentFilter();
85+
filter.addAction(Intent.ACTION_CREATE_SHORTCUT);
86+
filter.addAction("com.android.launcher.action.INSTALL_SHORTCUT");
87+
filter.addAction("android.content.pm.action.CONFIRM_PIN_SHORTCUT");
88+
filter.addAction(Intent.ACTION_VIEW);
89+
90+
receiver = new ShortcutReceiver();
91+
registerReceiver(receiver,filter);
92+
93+
}
94+
6895
@Override
6996
protected void onResume() {
7097
super.onResume();
@@ -77,6 +104,9 @@ protected void onResume() {
77104
@Override
78105
protected void onDestroy() {
79106
super.onDestroy();
107+
if (receiver != null){
108+
unregisterReceiver(receiver);
109+
}
80110
EventBus.getDefault().unregister(this);
81111
}
82112

@@ -93,6 +123,7 @@ private void runShortcut() {
93123
}
94124
}
95125

126+
QPyScriptModel mBean;
96127
private void initView() {
97128
dataList = new ArrayList<>();
98129
adapter = new AppListAdapter(dataList, getIntent().getStringExtra("type"), this);
@@ -107,6 +138,15 @@ public void runProject(QPyScriptModel item) {
107138
ScriptExec.getInstance().playProject(AppListActivity.this, item.getPath(), false);
108139
}
109140

141+
@Override
142+
public void createShortcut(QPyScriptModel item) {
143+
mBean = item;
144+
// if (!checkPermission()){
145+
// return;
146+
// }
147+
createShortcutOnThis();
148+
}
149+
110150
@Override
111151
public void exit() {
112152
AppListActivity.this.finish();
@@ -124,6 +164,78 @@ public void exit() {
124164
getScriptList();
125165
}
126166

167+
// private boolean checkPermission() {
168+
// if (Build.VERSION.SDK_INT >= 23) {
169+
// int checkPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.INSTALL_SHORTCUT);
170+
// LogUtil.e("checkPermission" + checkPermission);
171+
// LogUtil.e("checkPermission" + PackageManager.PERMISSION_GRANTED);
172+
// if (checkPermission != PackageManager.PERMISSION_GRANTED) {
173+
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INSTALL_SHORTCUT}, REQUEST_INSTALL_SHORTCUT);
174+
// return false;
175+
// } else {
176+
// return true;
177+
// }
178+
// } else {
179+
// return true;
180+
// }
181+
// }
182+
183+
// @Override
184+
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
185+
// if (requestCode == REQUEST_INSTALL_SHORTCUT) {
186+
// if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
187+
// Toast.makeText(this, R.string.toast_read_permission_deny, Toast.LENGTH_SHORT).show();
188+
// } else {
189+
// createShortcutOnThis();
190+
// }
191+
// }
192+
// }
193+
194+
private void createShortcutOnThis(){
195+
if (mBean == null){
196+
return;
197+
}
198+
199+
Intent intent = new Intent();
200+
intent.setClass(this, AppListActivity.class);
201+
intent.setAction(Intent.ACTION_VIEW);
202+
intent.putExtra("type", "script");
203+
intent.putExtra("path", mBean.getPath());
204+
intent.putExtra("isProj", mBean.isProj());
205+
206+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
207+
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
208+
if (mShortcutManager.isRequestPinShortcutSupported()) {
209+
ShortcutInfo pinShortcutInfo =
210+
new ShortcutInfo.Builder(this, mBean.getLabel())
211+
.setShortLabel(mBean.getLabel())
212+
.setLongLabel(mBean.getLabel())
213+
.setIcon(Icon.createWithResource(this, mBean.getIconRes()))
214+
.setIntent(intent)
215+
.build();
216+
Intent pinnedShortcutCallbackIntent =
217+
mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
218+
PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
219+
pinnedShortcutCallbackIntent, 0);
220+
221+
mShortcutManager.requestPinShortcut(pinShortcutInfo,
222+
successCallback.getIntentSender());
223+
}
224+
} else {
225+
//Adding shortcut for MainActivity
226+
//on Home screen
227+
Intent addIntent = new Intent();
228+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
229+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mBean.getLabel());
230+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
231+
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
232+
mBean.getIconRes()));
233+
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
234+
getApplicationContext().sendBroadcast(addIntent);
235+
Toast.makeText(this, getString(R.string.shortcut_create_suc, mBean.getLabel()), Toast.LENGTH_SHORT).show();
236+
}
237+
}
238+
127239

128240
private void getScriptList() {
129241
try {

qpython/src/main/java/org/qpython/qpy/main/adapter/AppListAdapter.java

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.qpython.qpy.main.model.QPyScriptModel;
2929
import org.qpython.qpy.texteditor.EditorActivity;
3030
import org.qpython.qpy.texteditor.ui.view.EnterDialog;
31+
import org.qpython.qsl4a.qsl4a.LogUtil;
32+
3133
import android.support.v7.app.AlertDialog;
3234

3335

@@ -114,6 +116,8 @@ public void onBindViewHolder(MyViewHolder<ItemAppListBinding> holder, int positi
114116
case 2:
115117
openToEdit(position);
116118
dialog.dismiss();
119+
default:
120+
break;
117121

118122
}
119123
}).setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
@@ -146,6 +150,8 @@ public interface Callback {
146150

147151
void runProject(QPyScriptModel item);
148152

153+
void createShortcut(QPyScriptModel item);
154+
149155
void exit();
150156
}
151157

@@ -156,43 +162,46 @@ private boolean createShortCut(int position) {
156162
}
157163
// Create shortcut
158164
QPyScriptModel qPyScriptModel = (QPyScriptModel) dataList.get(position);
159-
Intent intent = new Intent();
160-
intent.setClass(context, AppListActivity.class);
161-
intent.setAction(Intent.ACTION_VIEW);
162-
intent.putExtra("type", "script");
163-
intent.putExtra("path", qPyScriptModel.getPath());
164-
intent.putExtra("isProj", qPyScriptModel.isProj());
165-
166-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
167-
ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
168-
if (mShortcutManager.isRequestPinShortcutSupported()) {
169-
ShortcutInfo pinShortcutInfo =
170-
new ShortcutInfo.Builder(context, dataList.get(position).getLabel())
171-
.setShortLabel(dataList.get(position).getLabel())
172-
.setLongLabel(dataList.get(position).getLabel())
173-
.setIcon(Icon.createWithResource(context, dataList.get(position).getIconRes()))
174-
.setIntent(intent)
175-
.build();
176-
Intent pinnedShortcutCallbackIntent =
177-
mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
178-
PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,
179-
pinnedShortcutCallbackIntent, 0);
180-
mShortcutManager.requestPinShortcut(pinShortcutInfo,
181-
successCallback.getIntentSender());
182-
}
183-
} else {
184-
//Adding shortcut for MainActivity
185-
//on Home screen
186-
Intent addIntent = new Intent();
187-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
188-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, dataList.get(position).getLabel());
189-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
190-
Intent.ShortcutIconResource.fromContext(context.getApplicationContext(),
191-
dataList.get(position).getIconRes()));
192-
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
193-
context.getApplicationContext().sendBroadcast(addIntent);
194-
Toast.makeText(context, context.getString(R.string.shortcut_create_suc, dataList.get(position).getLabel()), Toast.LENGTH_SHORT).show();
195-
}
165+
// Intent intent = new Intent();
166+
// intent.setClass(context, AppListActivity.class);
167+
// intent.setAction(Intent.ACTION_VIEW);
168+
// intent.putExtra("type", "script");
169+
// intent.putExtra("path", qPyScriptModel.getPath());
170+
// intent.putExtra("isProj", qPyScriptModel.isProj());
171+
//
172+
// if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
173+
// ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
174+
// if (mShortcutManager.isRequestPinShortcutSupported()) {
175+
// ShortcutInfo pinShortcutInfo =
176+
// new ShortcutInfo.Builder(context, dataList.get(position).getLabel())
177+
// .setShortLabel(dataList.get(position).getLabel())
178+
// .setLongLabel(dataList.get(position).getLabel())
179+
// .setIcon(Icon.createWithResource(context, dataList.get(position).getIconRes()))
180+
// .setIntent(intent)
181+
// .build();
182+
// Intent pinnedShortcutCallbackIntent =
183+
// mShortcutManager.createShortcutResultIntent(pinShortcutInfo);
184+
// PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,
185+
// pinnedShortcutCallbackIntent, 0);
186+
//
187+
// boolean aaa = mShortcutManager.requestPinShortcut(pinShortcutInfo,
188+
// successCallback.getIntentSender());
189+
// LogUtil.e("11111111111" + aaa);
190+
// }
191+
// } else {
192+
// //Adding shortcut for MainActivity
193+
// //on Home screen
194+
// Intent addIntent = new Intent();
195+
// addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
196+
// addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, dataList.get(position).getLabel());
197+
// addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
198+
// Intent.ShortcutIconResource.fromContext(context.getApplicationContext(),
199+
// dataList.get(position).getIconRes()));
200+
// addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
201+
// context.getApplicationContext().sendBroadcast(addIntent);
202+
// Toast.makeText(context, context.getString(R.string.shortcut_create_suc, dataList.get(position).getLabel()), Toast.LENGTH_SHORT).show();
203+
// }
204+
callback.createShortcut(qPyScriptModel);
196205
return true;
197206
}
198207

qpython/src/main/java/org/qpython/qpy/main/app/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ private void initCactus() {
241241
.isDebug(true)
242242
.setTitle("QPython")
243243
.setContent("QPython service is alive")
244+
.setLargeIcon(R.drawable.ic_launcher)
245+
.setSmallIcon(R.drawable.ic_launcher)
244246
.hideNotificationAfterO(false)
245247
.addCallback(new CactusCallback() {
246248
@Override

0 commit comments

Comments
 (0)