Skip to content

Commit

Permalink
ankidroid#5740 - Add Dialog Requesting Permissions
Browse files Browse the repository at this point in the history
Some users din't feel comfortable about us requiring access to
external storage.

We explain that we don't perform any malicious activities with this
in order to hopefully alleviate concerns.
  • Loading branch information
david-allison committed Mar 21, 2020
1 parent 75d59fd commit e86bec7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
36 changes: 33 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.provider.Settings;

import com.afollestad.materialdialogs.GravityEnum;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.core.app.ShareCompat;
Expand Down Expand Up @@ -195,6 +196,8 @@ public class DeckPicker extends NavigationDrawerActivity implements
*/
private long mFocusedDeck;

/** If we have accepted the "We will show you permissions" dialog, don't show it again on activity rebirth */
private boolean mClosedWelcomeMessage;

// ----------------------------------------------------------------------------
// LISTENERS
Expand Down Expand Up @@ -364,6 +367,9 @@ public void onPostExecute(DeckTask.TaskData result) {
protected void onCreate(Bundle savedInstanceState) throws SQLException {
Timber.d("onCreate()");
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());

//we need to restore here, as we need it before super.onCreate() is called.
restoreWelcomeMessage(savedInstanceState);
// Open Collection on UI thread while splash screen is showing
boolean colOpen = firstCollectionOpen();

Expand Down Expand Up @@ -454,11 +460,28 @@ private boolean firstCollectionOpen() {
if (CollectionHelper.hasStorageAccessPermission(this)) {
// Show error dialog if collection could not be opened
return CollectionHelper.getInstance().getColSafe(this) != null;
} else {
// Request storage permission if we don't have it (e.g. on Android 6.0+)
} else if (mClosedWelcomeMessage) {
// DEFECT #5847: This fails if the activity is killed.
//Even if the dialog is showing, we want to show it again.
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_STORAGE_PERMISSION);
return false;
} else {
// Request storage permission if we don't have it (e.g. on Android 6.0+)
new MaterialDialog.Builder(this)
.title(R.string.collection_load_welcome_request_permissions_title)
.titleGravity(GravityEnum.CENTER)
.content(R.string.collection_load_welcome_request_permissions_details)
.positiveText(R.string.dialog_ok)
.onPositive((innerDialog, innerWhich) -> {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_STORAGE_PERMISSION);
this.mClosedWelcomeMessage = true;
})
.cancelable(false)
.canceledOnTouchOutside(false)
.show();
return false;
}
}

Expand Down Expand Up @@ -747,6 +770,7 @@ protected void onResume() {
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putLong("mContextMenuDid", mContextMenuDid);
savedInstanceState.putBoolean("mClosedWelcomeMessage", mClosedWelcomeMessage);
}


Expand Down Expand Up @@ -843,6 +867,12 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
// CUSTOM METHODS
// ----------------------------------------------------------------------------

private void restoreWelcomeMessage(Bundle savedInstanceState) {
if (savedInstanceState == null) {
return;
}
mClosedWelcomeMessage = savedInstanceState.getBoolean("mClosedWelcomeMessage");
}

/**
* Perform the following tasks:
Expand Down
4 changes: 4 additions & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@
<string name="empty_cards_none">No empty cards</string>
<string name="empty_cards_count">Cards to delete: %d</string>
<string name="empty_cards_deleted">Cards deleted: %d</string>

<!-- Initial collection load -->
<string name="collection_load_welcome_request_permissions_title">Welcome to AnkiDroid!</string>
<string name="collection_load_welcome_request_permissions_details">We require storage access to store your AnkiDroid collection, including media and backups. We do not access any of your media or files. Our code is open-source, written by volunteers, and trusted by millions.\n\nIf you have any questions, please access our in-app manual or visit our support forums.\n\nThank you for trying AnkiDroid!\n—AnkiDroid Development Team</string>
</resources>

0 comments on commit e86bec7

Please sign in to comment.