|
1 | 1 | package com.blogspot.developersu.ns_usbloader; |
2 | 2 |
|
| 3 | +import static android.Manifest.permission.ACCESS_LOCAL_NETWORK; |
3 | 4 | import static android.Manifest.permission.POST_NOTIFICATIONS; |
4 | 5 | import static com.blogspot.developersu.ns_usbloader.NsConstants.DEFAULT_NS_IP; |
5 | 6 | import static com.blogspot.developersu.ns_usbloader.NsConstants.DEFAULT_PHONE_IP; |
@@ -77,6 +78,7 @@ public class MainActivity extends AppCompatActivity implements |
77 | 78 | public static final boolean IS_AFTER_KIT_KAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; |
78 | 79 | public static final boolean IS_AFTER_TIRAMISU = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU; |
79 | 80 | public static final boolean IS_AFTER_JELLY_BEAN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; |
| 81 | + public static final boolean IS_AFTER_CINNAMON_BUN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.CINNAMON_BUN; |
80 | 82 |
|
81 | 83 | private RecyclerView recyclerView; |
82 | 84 | private NspItemsAdapter nspItemsAdapter; |
@@ -257,6 +259,7 @@ public void handleOnBackPressed() { |
257 | 259 | readFile(getIntent()); |
258 | 260 |
|
259 | 261 | requestNotificationsPermission(); |
| 262 | + requestIntranetPermission(); |
260 | 263 | } |
261 | 264 |
|
262 | 265 | private boolean requestNotificationsPermission() { |
@@ -284,6 +287,31 @@ private boolean requestNotificationsPermission() { |
284 | 287 | return true; |
285 | 288 | } |
286 | 289 |
|
| 290 | + private boolean requestIntranetPermission() { |
| 291 | + if (!IS_AFTER_CINNAMON_BUN) |
| 292 | + return false; |
| 293 | + |
| 294 | + if (ActivityCompat.checkSelfPermission(this, ACCESS_LOCAL_NETWORK) == PackageManager.PERMISSION_GRANTED) |
| 295 | + return false; |
| 296 | + |
| 297 | + if (shouldShowRequestPermissionRationale(ACCESS_LOCAL_NETWORK)) { |
| 298 | + Snackbar.make(getWindow().getDecorView(), R.string.application_needs_local_net, BaseTransientBottomBar.LENGTH_LONG) |
| 299 | + .setAction(R.string.settings, view -> { |
| 300 | + Intent intent = new Intent(); |
| 301 | + intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 302 | + intent.setData( |
| 303 | + Uri.fromParts("package", getPackageName(), null)); |
| 304 | + startActivity(intent); |
| 305 | + }) |
| 306 | + .show(); |
| 307 | + } |
| 308 | + else { |
| 309 | + ActivityCompat.requestPermissions(this, new String[]{ACCESS_LOCAL_NETWORK}, 1); |
| 310 | + } |
| 311 | + |
| 312 | + return true; |
| 313 | + } |
| 314 | + |
287 | 315 | private void updateUploadBtnState(){ // TODO: this function is bad. It multiplies entropy and sorrow. |
288 | 316 | uploadToNsBtn.setEnabled(nspItemsAdapter.getItemCount() > 0); |
289 | 317 | } |
@@ -382,6 +410,9 @@ private void uploadFiles() { |
382 | 410 | serviceStartIntent.putExtra(NSS_PROTOCOL, selector.getSelected()); |
383 | 411 |
|
384 | 412 | if (selector.isNet()) { |
| 413 | + if (requestIntranetPermission()) |
| 414 | + return; |
| 415 | + |
385 | 416 | SharedPreferences sp = getSharedPreferences("NSUSBloader", MODE_PRIVATE); |
386 | 417 |
|
387 | 418 | serviceStartIntent.putExtra(NSS_NS_IP, sp.getString("SNsIP", DEFAULT_NS_IP)); |
|
0 commit comments