Merge "Reload data after the permission is granted"
am: 935203f1c6

* commit '935203f1c663f3325ba03cffee8482340117faf7':
  Reload data after the permission is granted
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5f3a124..28b5a6f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -18,5 +18,6 @@
     <!-- Title of the HTML Viewer activity. -->
     <string name="app_label">HTML Viewer</string>
     <string name="cannot_open_link">No application can open this link.</string>
+    <string name="turn_on_storage_permission">Please turn on Storage permission in app permissions.</string>
 </resources>
 
diff --git a/src/com/android/htmlviewer/HTMLViewerActivity.java b/src/com/android/htmlviewer/HTMLViewerActivity.java
index e31e4d4..aca339f 100644
--- a/src/com/android/htmlviewer/HTMLViewerActivity.java
+++ b/src/com/android/htmlviewer/HTMLViewerActivity.java
@@ -49,7 +49,7 @@
 
     private WebView mWebView;
     private View mLoading;
-    private Uri mOnPermissionDestination;
+    private Intent mIntent;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -77,24 +77,28 @@
         s.setJavaScriptEnabled(false);
         s.setDefaultTextEncodingName("utf-8");
 
-        final Intent intent = getIntent();
-        if (intent.hasExtra(Intent.EXTRA_TITLE)) {
-            setTitle(intent.getStringExtra(Intent.EXTRA_TITLE));
-        }
+        mIntent = getIntent();
+        requestPermissionAndLoad();
+    }
 
-        Uri destination = intent.getData();
+    private void loadUrl() {
+        if (mIntent.hasExtra(Intent.EXTRA_TITLE)) {
+            setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE));
+        }
+        mWebView.loadUrl(String.valueOf(mIntent.getData()));
+    }
+
+    private void requestPermissionAndLoad() {
+        Uri destination = mIntent.getData();
         if (destination != null) {
             // Is this a local file?
-            if ("file".equals(destination.getScheme())) {
-                if (PackageManager.PERMISSION_DENIED ==
-                        checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
-                    // If we don't have local file permissions, save the destination so we can try
-                    // again once they're granted.
-                    mOnPermissionDestination = destination;
-                    requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
-                }
+            if ("file".equals(destination.getScheme())
+                        && PackageManager.PERMISSION_DENIED ==
+                                checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
+                requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
+            } else {
+                loadUrl();
             }
-            mWebView.loadUrl(destination.toString());
         }
     }
 
@@ -108,7 +112,11 @@
 
         if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
             // Try again now that we have the permission.
-            mWebView.loadUrl(mOnPermissionDestination.toString());
+            loadUrl();
+        } else {
+            Toast.makeText(HTMLViewerActivity.this,
+                    R.string.turn_on_storage_permission, Toast.LENGTH_SHORT).show();
+            finish();
         }
     }