Skip to content

Commit

Permalink
Redirect user to settings if no permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
bqqbarbhg committed Mar 14, 2016
1 parent ad4d258 commit 7172a60
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fi.aalto.legroup.achso.browsing;

import android.Manifest;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
Expand All @@ -15,6 +15,7 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
Expand All @@ -25,6 +26,8 @@
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.astuetz.PagerSlidingTabStrip;
import com.melnykov.fab.FloatingActionButton;
import com.melnykov.fab.ScrollDirectionListener;
Expand Down Expand Up @@ -73,6 +76,7 @@ public final class BrowserActivity extends BaseActivity implements View.OnClickL

private static final int ACH_SO_TAKE_VIDEO_PERM = 3;
private static final int ACH_SO_LOG_IN_PERM = 4;
private static final int ACH_SO_BROWSE_PERM = 5;

private static final String STATE_VIDEO_BUILDER = "STATE_VIDEO_BUILDER";
private static final String ARG_LAYERS_BOX_URL = "ARG_LAYERS_BOX_URL";
Expand Down Expand Up @@ -146,6 +150,12 @@ protected void onCreate(Bundle savedInstanceState) {

// Control the media volume instead of the ringer volume
setVolumeControlStream(AudioManager.STREAM_MUSIC);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
}, ACH_SO_BROWSE_PERM);
}
}

@Override
Expand Down Expand Up @@ -443,14 +453,14 @@ public void onLoginState(LoginStateEvent event) {

case LOGGED_OUT:
showSnackbar(R.string.logged_out);

// Sync removes cached videos
App.videoRepository.forceNextSyncImportant();
SyncService.syncWithCloudStorage(this);
break;
}
}


swipeRefreshLayout.setRefreshing(true);
SyncService.syncWithCloudStorage(this);

invalidateOptionsMenu();
}

Expand Down Expand Up @@ -523,30 +533,60 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
// We need all three permissions (Fine location, using the camera, writing to the filesystem
// Otherwise we just show a toast and exit.
if (requestCode == ACH_SO_TAKE_VIDEO_PERM) {
if (grantResults.length == 4) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
showSnackbar(R.string.video_no_permissions);
return;
}
}
recordVideo();
} else {
showSnackbar(R.string.video_no_permissions);
}
if (!checkPermissions(R.string.video_no_permissions, permissions, grantResults))
return;

recordVideo();

} else if (requestCode == ACH_SO_LOG_IN_PERM) {
if (grantResults.length != 2)
if (!checkPermissions(R.string.log_in_no_permissions, permissions, grantResults))
return;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
showSnackbar(R.string.video_no_permissions);
return;
}
}

Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
} else if (requestCode == ACH_SO_BROWSE_PERM) {
if (!checkPermissions(R.string.browse_no_permissions, permissions, grantResults))
return;

SyncService.syncWithCloudStorage(this);
}
}

private boolean checkPermissions(int messageResource, String permissions[], int[] grantResults) {
boolean hasPermissions = true;

if (permissions.length == grantResults.length) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
hasPermissions = false;
break;
}
}
} else {
hasPermissions = false;
}

if (!hasPermissions) {

MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(messageResource)
.negativeText(R.string.cancel)
.positiveText(R.string.go_to_settings)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myAppSettings);
}
})
.build();
dialog.show();

}

return hasPermissions;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
<string name="search_hint">Search for videos</string>

<string name="video_no_permissions">Could not take video due to permissions</string>
<string name="log_in_no_permissions">Could not log in due to permissions</string>
<string name="browse_no_permissions">Could not show videos due to permissions</string>
<string name="empty_results">No matching videos</string>
<string name="import_video">Import video</string>
<string name="video_author">Author</string>
Expand Down Expand Up @@ -148,6 +150,7 @@
<string name="only_public_layers_box_description">Only the public Layers Box is supported at the moment.</string>
<string name="warning">Warning</string>
<string name="public_layers_box_chosen_warning">Setting changed but still using public Layers Box, do you want to choose to use the private URL instead?</string>
<string name="go_to_settings">Go to settings</string>

<string-array name="settings_annotation_pause_duration">
<item name="1000">Fast</item>
Expand Down

0 comments on commit 7172a60

Please sign in to comment.