|
4 | 4 | import android.app.Activity; |
5 | 5 | import android.app.AlertDialog; |
6 | 6 | import android.content.Context; |
7 | | -import android.content.Intent; |
8 | | -import android.content.pm.ShortcutInfo; |
9 | 7 | import android.content.pm.ShortcutManager; |
10 | | -import android.graphics.drawable.BitmapDrawable; |
11 | | -import android.graphics.drawable.Drawable; |
12 | | -import android.graphics.drawable.Icon; |
13 | | -import android.net.Uri; |
14 | 8 | import android.os.Build; |
15 | 9 | import android.os.Bundle; |
16 | 10 | import android.view.MenuItem; |
17 | 11 | import android.widget.ArrayAdapter; |
18 | 12 | import android.widget.ListView; |
19 | 13 |
|
20 | | -import androidx.annotation.Nullable; |
21 | | - |
22 | | -import com.google.common.base.Joiner; |
23 | 14 | import com.termux.shared.data.DataUtils; |
24 | | -import com.termux.shared.file.FileUtils; |
25 | | -import com.termux.shared.file.TermuxFileUtils; |
26 | | -import com.termux.shared.file.filesystem.FileType; |
27 | 15 | import com.termux.shared.logger.Logger; |
28 | | -import com.termux.shared.settings.preferences.TermuxWidgetAppSharedPreferences; |
29 | | -import com.termux.shared.shell.ShellUtils; |
30 | 16 | import com.termux.shared.termux.TermuxConstants; |
31 | | -import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE; |
32 | | -import com.termux.shared.termux.TermuxConstants.TERMUX_WIDGET; |
33 | 17 | import com.termux.shared.termux.TermuxUtils; |
34 | 18 | import com.termux.widget.utils.ShortcutUtils; |
35 | 19 |
|
@@ -124,102 +108,36 @@ private void createShortcut(Context context, File clickedFile) { |
124 | 108 | isPinnedShortcutSupported = true; |
125 | 109 | } |
126 | 110 |
|
127 | | - String shortcutFilePath = FileUtils.getCanonicalPath(clickedFile.getAbsolutePath(), null); |
| 111 | + ShortcutFile shortcutFile = new ShortcutFile(clickedFile); |
128 | 112 |
|
129 | 113 | try { |
130 | 114 | if (isPinnedShortcutSupported) |
131 | | - createPinnedShortcut(context, shortcutFilePath); |
| 115 | + createPinnedShortcut(context, shortcutFile); |
132 | 116 | else |
133 | | - createStaticShortcut(context, shortcutFilePath); |
| 117 | + createStaticShortcut(context, shortcutFile); |
134 | 118 | } catch (Exception e) { |
135 | 119 | String message = context.getString( |
136 | 120 | isPinnedShortcutSupported ? R.string.error_create_pinned_shortcut_failed : R.string.error_create_static_shortcut_failed, |
137 | | - TermuxFileUtils.getUnExpandedTermuxPath(shortcutFilePath)); |
| 121 | + shortcutFile.getUnExpandedPath()); |
138 | 122 | Logger.logErrorAndShowToast(context, LOG_TAG, message + ": " + e.getMessage()); |
139 | 123 | Logger.logStackTraceWithMessage(LOG_TAG, message, e); |
140 | 124 | } |
141 | 125 | } |
142 | 126 |
|
143 | 127 | @TargetApi(Build.VERSION_CODES.O) |
144 | | - private void createPinnedShortcut(Context context, String shortcutFilePath) { |
| 128 | + private void createPinnedShortcut(Context context, ShortcutFile shortcutFile) { |
145 | 129 | ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); |
146 | 130 | if (shortcutManager == null) return; |
147 | 131 |
|
148 | | - String shortcutFileName = ShellUtils.getExecutableBasename(shortcutFilePath); |
149 | | - |
150 | | - ShortcutInfo.Builder builder = new ShortcutInfo.Builder(context, shortcutFilePath); |
151 | | - builder.setIntent(TermuxCreateShortcutActivity.getExecutionIntent(context, shortcutFilePath)); |
152 | | - builder.setShortLabel(shortcutFileName); |
153 | | - |
154 | | - File shortcutIconFile = TermuxCreateShortcutActivity.getShortcutIconFile(context, shortcutFileName); |
155 | | - if (shortcutIconFile != null) |
156 | | - builder.setIcon(Icon.createWithBitmap(((BitmapDrawable) Drawable.createFromPath(shortcutIconFile.getAbsolutePath())).getBitmap())); |
157 | | - else |
158 | | - builder.setIcon(Icon.createWithResource(context, R.drawable.ic_launcher)); |
159 | | - |
160 | 132 | Logger.showToast(context, context.getString(R.string.msg_request_create_pinned_shortcut, |
161 | | - TermuxFileUtils.getUnExpandedTermuxPath(shortcutFilePath)), true); |
162 | | - shortcutManager.requestPinShortcut(builder.build(), null); |
| 133 | + shortcutFile.getUnExpandedPath()), true); |
| 134 | + shortcutManager.requestPinShortcut(shortcutFile.getShortcutInfo(context), null); |
163 | 135 | } |
164 | 136 |
|
165 | | - private void createStaticShortcut(Context context, String shortcutFilePath) { |
166 | | - String shortcutFileName = ShellUtils.getExecutableBasename(shortcutFilePath); |
167 | | - |
168 | | - Intent intent = new Intent(); |
169 | | - intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, TermuxCreateShortcutActivity.getExecutionIntent(context, shortcutFilePath)); |
170 | | - intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutFileName); |
171 | | - |
172 | | - File shortcutIconFile = TermuxCreateShortcutActivity.getShortcutIconFile(context, shortcutFileName); |
173 | | - if (shortcutIconFile != null) |
174 | | - intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, ((BitmapDrawable) Drawable.createFromPath(shortcutIconFile.getAbsolutePath())).getBitmap()); |
175 | | - else |
176 | | - intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher)); |
177 | | - |
| 137 | + private void createStaticShortcut(Context context, ShortcutFile shortcutFile) { |
178 | 138 | Logger.showToast(context, context.getString(R.string.msg_request_create_static_shortcut, |
179 | | - TermuxFileUtils.getUnExpandedTermuxPath(shortcutFilePath)), true); |
180 | | - setResult(RESULT_OK, intent); |
181 | | - } |
182 | | - |
183 | | - public static Intent getExecutionIntent(Context context, String shortcutFilePath) { |
184 | | - Uri scriptUri = new Uri.Builder().scheme(TERMUX_SERVICE.URI_SCHEME_SERVICE_EXECUTE).path(shortcutFilePath).build(); |
185 | | - Intent executionIntent = new Intent(context, TermuxLaunchShortcutActivity.class); |
186 | | - executionIntent.setAction(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE); // Mandatory for pinned shortcuts |
187 | | - executionIntent.setData(scriptUri); |
188 | | - executionIntent.putExtra(TERMUX_WIDGET.EXTRA_TOKEN_NAME, TermuxWidgetAppSharedPreferences.getGeneratedToken(context)); |
189 | | - return executionIntent; |
190 | | - } |
191 | | - |
192 | | - @Nullable |
193 | | - public static File getShortcutIconFile(Context context, String shortcutFileName) { |
194 | | - String errmsg; |
195 | | - String shortcutIconFilePath = FileUtils.getCanonicalPath( |
196 | | - TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH + |
197 | | - "/" + shortcutFileName + ".png", null); |
198 | | - |
199 | | - FileType fileType = FileUtils.getFileType(shortcutIconFilePath, true); |
200 | | - // Ensure file or symlink points to a regular file that exists |
201 | | - if (fileType != FileType.REGULAR) { |
202 | | - if (fileType != FileType.NO_EXIST) { |
203 | | - errmsg = context.getString(R.string.error_icon_not_a_regular_file, fileType.getName()) + |
204 | | - "\n" + context.getString(R.string.msg_icon_absolute_path, shortcutIconFilePath); |
205 | | - Logger.logErrorAndShowToast(context, LOG_TAG, errmsg); |
206 | | - } |
207 | | - return null; |
208 | | - } |
209 | | - |
210 | | - // Do not allow shortcut icons files not under SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST |
211 | | - if (!FileUtils.isPathInDirPaths(shortcutIconFilePath, ShortcutUtils.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST, true)) { |
212 | | - errmsg = context.getString(R.string.error_icon_not_under_shortcut_icons_directories, |
213 | | - Joiner.on(", ").skipNulls().join(TermuxFileUtils.getUnExpandedTermuxPaths(ShortcutUtils.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST))) + |
214 | | - "\n" + context.getString(R.string.msg_icon_absolute_path, shortcutIconFilePath); |
215 | | - Logger.logErrorAndShowToast(context, LOG_TAG, errmsg); |
216 | | - return null; |
217 | | - } |
218 | | - |
219 | | - Logger.logInfo(LOG_TAG, "Using file at \"" + shortcutIconFilePath + "\" as shortcut icon file"); |
220 | | - Logger.showToast(context, "Using file at \"" + shortcutIconFilePath + "\" as shortcut icon file", true); |
221 | | - |
222 | | - return new File(shortcutIconFilePath); |
| 139 | + shortcutFile.getUnExpandedPath()), true); |
| 140 | + setResult(RESULT_OK, shortcutFile.getStaticShortcutIntent(context)); |
223 | 141 | } |
224 | 142 |
|
225 | 143 | } |
0 commit comments