11package org .qpython .qpy .main .activity ;
22
3- import android .app . Activity ;
3+ import android .Manifest ;
44import android .app .AlertDialog ;
55import android .app .LoaderManager ;
6+ import android .app .PendingIntent ;
67import android .content .Context ;
78import android .content .Intent ;
9+ import android .content .IntentFilter ;
810import 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 ;
916import android .os .Bundle ;
17+ import android .support .annotation .NonNull ;
1018import 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 ;
1221import android .support .v7 .widget .GridLayoutManager ;
1322import android .support .v7 .widget .LinearLayoutManager ;
1423import android .support .v7 .widget .RecyclerView ;
15- import android .support .v7 .widget .Toolbar ;
16- import android .view .Gravity ;
17- import android .widget .LinearLayout ;
1824import android .widget .TextView ;
25+ import android .widget .Toast ;
1926
2027import com .quseit .util .FileHelper ;
2128import com .quseit .util .FolderUtils ;
2633import org .greenrobot .eventbus .ThreadMode ;
2734import org .qpython .qpy .R ;
2835import org .qpython .qpy .console .ScriptExec ;
36+ import org .qpython .qpy .console .shortcuts .ShortcutReceiver ;
2937import org .qpython .qpy .main .adapter .AppListAdapter ;
3038import org .qpython .qpy .main .event .AppsLoader ;
3139import org .qpython .qpy .main .model .AppModel ;
3240import org .qpython .qpy .main .model .QPyScriptModel ;
3341import org .qpython .qpysdk .QPyConstants ;
3442import org .qpython .qpysdk .utils .Utils ;
43+ import org .qpython .qsl4a .qsl4a .LogUtil ;
3544
3645import java .io .File ;
3746import java .io .IOException ;
3847import java .util .ArrayList ;
3948import java .util .Arrays ;
4049import 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
4758public 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 {
0 commit comments