11package com .nononsenseapps .notepad .widget .shortcut ;
22
33import android .content .Intent ;
4+ import android .content .pm .ShortcutInfo ;
5+ import android .content .pm .ShortcutManager ;
46import android .database .Cursor ;
57import android .graphics .Bitmap ;
68import android .graphics .Canvas ;
79import android .graphics .drawable .AdaptiveIconDrawable ;
810import android .graphics .drawable .Drawable ;
11+ import android .graphics .drawable .Icon ;
12+ import android .os .Build ;
913import android .os .Bundle ;
1014import android .widget .SimpleCursorAdapter ;
1115import android .widget .Spinner ;
1923import androidx .loader .content .Loader ;
2024
2125import com .nononsenseapps .helpers .ActivityHelper ;
26+ import com .nononsenseapps .helpers .NnnLogger ;
2227import com .nononsenseapps .helpers .ThemeHelper ;
2328import com .nononsenseapps .notepad .R ;
2429import com .nononsenseapps .notepad .activities .main .ActivityMain_ ;
@@ -70,6 +75,57 @@ private static Bitmap getBitmapFromDrawable(@NonNull Drawable drawable) {
7075 }
7176
7277 void onOK () {
78+ // newer android versions have stricter limits on nested Intents
79+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
80+
81+ ShortcutManager shortcutManager = this .getSystemService (ShortcutManager .class );
82+
83+ String shortcutTitle = "" ;
84+ final Intent intent = new Intent ();
85+ if (mBinding .createNoteSwitch .isChecked ()) {
86+
87+ String listName = null ;
88+ final Cursor c = (Cursor ) mBinding .listSpinner .getSelectedItem ();
89+ if (c != null && !c .isClosed () && !c .isAfterLast ()) {
90+ listName = c .getString (1 );
91+ }
92+ if (listName == null ) {
93+ NnnLogger .error (ShortcutConfig .class , "Unexpected null in listName in ShortcutConfig.java" );
94+ }
95+
96+ shortcutTitle = listName + " - " + ShortcutConfig .this .getString (R .string .title_create );
97+
98+ intent .setClass (ShortcutConfig .this , ActivityMain_ .class )
99+ .setData (Task .URI )
100+ .setAction (Intent .ACTION_INSERT )
101+ .putExtra (Task .Columns .DBLIST , mBinding .listSpinner .getSelectedItemId ());
102+ } else {
103+ // this shortcut widget shows a list of notes
104+ final Cursor c = (Cursor ) mBinding .listSpinner .getSelectedItem ();
105+
106+ if (c != null && !c .isClosed () && !c .isAfterLast ()) {
107+ shortcutTitle = c .getString (1 );
108+ }
109+ intent .setClass (ShortcutConfig .this , ActivityMain_ .class )
110+ .setAction (Intent .ACTION_VIEW )
111+ .setData (TaskList .getUri (mBinding .listSpinner .getSelectedItemId ()));
112+ }
113+
114+ // widget IDs must be unique. We use unique titles for all widget combinations
115+ String shortcutId = shortcutTitle ;
116+ ShortcutInfo shortcut = new ShortcutInfo .Builder (this , shortcutId )
117+ .setShortLabel (shortcutTitle )
118+ .setLongLabel (shortcutTitle )
119+ .setIcon (Icon .createWithResource (this , R .drawable .app_icon ))
120+ .setIntent (intent )
121+ .build ();
122+
123+ shortcutManager .requestPinShortcut (shortcut , null );
124+ setResult (RESULT_OK );
125+ return ;
126+ }
127+
128+ // legacy code that still works for API 23 and 24
73129 final Intent shortcutIntent = new Intent ();
74130 // Set the icon for the shortcut widget
75131 Drawable iconDrawable = AppCompatResources .getDrawable (this , R .drawable .app_icon );
@@ -80,8 +136,7 @@ void onOK() {
80136 String shortcutTitle = "" ;
81137 final Intent intent = new Intent ();
82138 if (mBinding .createNoteSwitch .isChecked ()) {
83- shortcutTitle = ShortcutConfig .this
84- .getString (R .string .title_create );
139+ shortcutTitle = ShortcutConfig .this .getString (R .string .title_create );
85140
86141 intent .setClass (ShortcutConfig .this , ActivityMain_ .class )
87142 .setData (Task .URI )
@@ -118,26 +173,30 @@ private void setListEntries(final Spinner listSpinner) {
118173
119174 listSpinner .setAdapter (mSpinnerAdapter );
120175
121- LoaderManager .getInstance (this ).restartLoader (0 , null , new LoaderManager .LoaderCallbacks <Cursor >() {
122-
123- @ Override
124- public Loader <Cursor > onCreateLoader (int id , Bundle args ) {
125- return new CursorLoader (ShortcutConfig .this ,
126- TaskList .URI , new String [] {
127- TaskList .Columns ._ID ,
128- TaskList .Columns .TITLE }, null , null ,
129- TaskList .Columns .TITLE );
130- }
131-
132- @ Override
133- public void onLoadFinished (Loader <Cursor > arg0 , Cursor c ) {
134- mSpinnerAdapter .swapCursor (c );
135- }
136-
137- @ Override
138- public void onLoaderReset (Loader <Cursor > arg0 ) {
139- mSpinnerAdapter .swapCursor (null );
140- }
141- });
176+ LoaderManager
177+ .getInstance (this )
178+ .restartLoader (0 , null , new LoaderManager .LoaderCallbacks <Cursor >() {
179+
180+ @ NonNull
181+ @ Override
182+ public Loader <Cursor > onCreateLoader (int id , Bundle args ) {
183+ return new CursorLoader (ShortcutConfig .this ,
184+ TaskList .URI ,
185+ new String [] { TaskList .Columns ._ID , TaskList .Columns .TITLE },
186+ null ,
187+ null ,
188+ TaskList .Columns .TITLE );
189+ }
190+
191+ @ Override
192+ public void onLoadFinished (@ NonNull Loader <Cursor > arg0 , Cursor c ) {
193+ mSpinnerAdapter .swapCursor (c );
194+ }
195+
196+ @ Override
197+ public void onLoaderReset (@ NonNull Loader <Cursor > arg0 ) {
198+ mSpinnerAdapter .swapCursor (null );
199+ }
200+ });
142201 }
143202}
0 commit comments