Reload data after the permission is granted

HTMLViewer tries to load data even if the permission is denied.
It just shows up 'Webpage not available', so users can't notice
that this is the permission problem. It would be better to check
if the data has file scheme and show dialog to check permission.
HTMLViewer gets closed and shows toast message if permission is denied.
On the other hand, it reloads data if permission is granted by a user.

Change-Id: I26ad05136194cb36d4a17d3a8ab1a3fd2b5dfeb5
Signed-off-by: Euisang Lim <eui-sang.lim@samsung.com>
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();
         }
     }