Skip to content

Commit

Permalink
Show confirmation dialog if this is the only app to open the file
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed May 6, 2021
1 parent 931d8a6 commit ec092aa
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
android:maxSdkVersion="29"
tools:ignore="ScopedStorage" />

<queries>
<!-- Used to check if SaveCopy is the only one to open the file -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="content" />
</intent>
</queries>

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/java/app/rikka/savecopy/SaveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ protected void onCreate(Bundle savedInstanceState) {

getPackageManager().clearPackagePreferredActivities(getPackageName());

checkConfirmation();
}

private void checkConfirmation() {
if (shouldShowConfirmation()) {
new AlertDialog.Builder(this, R.style.AppTheme_Dialog_Alert)
.setMessage(R.string.dialog_confirmation_message)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, (dialog, which) -> checkPermission())
.setOnDismissListener(dialog -> finish())
.show();
return;
}
checkPermission();
}

private void checkPermission() {
if (Build.VERSION.SDK_INT >= 23 && (Build.VERSION.SDK_INT <= 28 || (Build.VERSION.SDK_INT == 29 && Build.VERSION.PREVIEW_SDK_INT == 0))) {
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED
Expand All @@ -35,10 +52,11 @@ protected void onCreate(Bundle savedInstanceState) {
return;
}
}
handleIntent();

startSave();
}

private void handleIntent() {
private void startSave() {
String callingPackage = null;
Uri referrer = getReferrer();
if (referrer != null) callingPackage = referrer.getAuthority();
Expand All @@ -62,7 +80,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
granted &= grantResult == PackageManager.PERMISSION_GRANTED;
}
if (granted) {
handleIntent();
startSave();
} else {
boolean isNight = (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_YES) > 0;
int theme = isNight ? android.R.style.Theme_DeviceDefault_Dialog_Alert : android.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
Expand All @@ -81,4 +99,15 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}
}

private boolean shouldShowConfirmation() {
try {
Intent intentForTest = new Intent(getIntent());
intentForTest.setComponent(null);
intentForTest.setPackage(null);
return getPackageManager().queryIntentActivities(intentForTest, PackageManager.MATCH_DEFAULT_ONLY).size() <= 1;
} catch (Throwable e) {
return true;
}
}
}
1 change: 0 additions & 1 deletion app/src/main/java/app/rikka/savecopy/SaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void onCreate() {
});

mNotificationManager = getSystemService(NotificationManager.class);
//noinspection ConstantConditions
onCreateNotificationChannel(mNotificationManager);

startForeground(NOTIFICATION_ID_PROGRESS, onStartForeground());
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/res/values-v30/theme.xml

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="settings_prefer_app_folder">偏好应用文件夹</string>
<string name="settings_prefer_app_folder_summary">将副本保存至 Download/&lt;应用名称&gt;。</string>
<string name="introduction">此应用将“%1$s”加入至“打开方式”菜单。当您使用“%1$s”打开文件时,文件的副本将被保存至“Download”文件夹。\n\n删除原始文件要求原始应用允许写入,但几乎没有允许写入的。因此我们没有提供此功能。\n\n为了保持主屏幕整洁,此应用不会在主屏幕应用中显示(Android 10 除外)。要卸载,请进入系统设置。</string>
</resources>
<string name="dialog_confirmation_message">是否要保存此文件的副本?</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="settings_prefer_app_folder">偏好應用程式資料夾</string>
<string name="settings_prefer_app_folder_summary">將副本儲存至 Download/&lt;應用程式名稱&gt;。</string>
<string name="introduction">此程式將「%1$s」加入至「開啟方式」選單。當您使用「%1$s」開啟檔案時,檔案的副本將被儲存至「Download」資料夾。\n\n刪除原始檔案要求原始程式允許寫入,但幾乎沒有允許寫入的。因此我們沒有提供此功能。\n\n為了保持主螢幕整潔,此程式不會在主螢幕程式中顯示(Android 10 除外)。要解除安裝,請進入系統設定。</string>
</resources>
<string name="dialog_confirmation_message">是否要儲存此檔案的副本?</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="settings_prefer_app_folder">Prefer app folder</string>
<string name="settings_prefer_app_folder_summary">Save the copy to Download/&lt;app name&gt;.</string>
<string name="introduction">This app adds the \"%1$s\" action to \"Open with\" menus. When you open a file with \"%1$s\", a copy of the file will be saved to the \"Download\" folder.\n\nDelete original file requires the original app to allow writing, but almost no one does. Therefore we did not provide this feature.\n\nIn order to keep your home clean, this app does not show in the home app (except Android 10). To uninstall, please go to system settings.</string>
</resources>
<string name="dialog_confirmation_message">Save a copy for this file?</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:enforceNavigationBarContrast">false</item>
<item name="android:enforceStatusBarContrast">false</item>
</style>

<style name="Theme.NoDisplay" parent="Theme.Translucent.NoTitleBar" />
Expand All @@ -19,4 +21,4 @@
<item name="android:windowActionBar">false</item>
</style>

</resources>
</resources>

0 comments on commit ec092aa

Please sign in to comment.