Add new implementation to view Regulatory Information

Change in this commit: HTMLViewer do not have capability
to handle a FileProvider URI, so map the URI to local path.

Issue: PRJ8901-744
Issue: FP3-A11#233
Change-Id: I1cf97254a2b104b5395927f32cf93c7e5023879a
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 655127b..378fedf 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -29,6 +29,7 @@
             <intent-filter>
                 <category android:name="android.intent.category.DEFAULT" />
                 <action android:name="android.intent.action.VIEW" />
+                <data android:scheme="file" />
                 <data android:scheme="content" />
                 <data android:mimeType="text/html"/>
                 <data android:mimeType="text/plain"/>
diff --git a/res/layout/elabellayout.xml b/res/layout/elabellayout.xml
new file mode 100644
index 0000000..a0ea6dc
--- /dev/null
+++ b/res/layout/elabellayout.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@*android:color/white">
+
+    <com.android.htmlviewer.FairphoneWebView
+        android:id="@+id/webview"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"/>
+
+    <ProgressBar
+        android:id="@+id/loading"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:indeterminate="true"
+        style="?android:attr/progressBarStyle" />
+
+</FrameLayout>
diff --git a/src/com/android/htmlviewer/FairphoneWebView.java b/src/com/android/htmlviewer/FairphoneWebView.java
new file mode 100644
index 0000000..7d2ef5d
--- /dev/null
+++ b/src/com/android/htmlviewer/FairphoneWebView.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2020-2022 Fairphone B.V.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.htmlviewer;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.webkit.WebView;
+
+public class FairphoneWebView extends WebView {
+    private static final String TAG = "FairphoneWebView";
+
+    public FairphoneWebView(Context context) {
+        super(context);
+    }
+
+    public FairphoneWebView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public FairphoneWebView(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    @Override
+    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
+        super.onScrollChanged(l, t, oldl, oldt);
+        Log.d(TAG, "onScrollChanged:l:" + l + ",t:" + t + ",oldl:" + oldl + ",oldt:" + oldt);
+        scrollTo(0, t);
+    }
+}
diff --git a/src/com/android/htmlviewer/HTMLViewerActivity.java b/src/com/android/htmlviewer/HTMLViewerActivity.java
index 1aa1f68..a7e50e1 100644
--- a/src/com/android/htmlviewer/HTMLViewerActivity.java
+++ b/src/com/android/htmlviewer/HTMLViewerActivity.java
@@ -34,6 +34,7 @@
 import android.webkit.WebViewClient;
 import android.widget.Toast;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
@@ -54,8 +55,14 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        mIntent = getIntent();
 
-        setContentView(R.layout.main);
+        String extra = mIntent.getStringExtra("extra.module");
+        if ("Regulatory information".equals(extra)) {
+            setContentView(R.layout.elabellayout);
+        } else {
+            setContentView(R.layout.main);
+        }
 
         mWebView = findViewById(R.id.webview);
         mLoading = findViewById(R.id.loading);
@@ -78,7 +85,11 @@
         s.setJavaScriptEnabled(false);
         s.setDefaultTextEncodingName("utf-8");
 
-        mIntent = getIntent();
+        if ("Regulatory information".equals(extra)) {
+            s.setSupportZoom(false);
+            s.setBuiltInZoomControls(false);
+        }
+
         setBackButton();
         loadUrl();
     }
@@ -87,7 +98,21 @@
         if (mIntent.hasExtra(Intent.EXTRA_TITLE)) {
             setTitle(mIntent.getStringExtra(Intent.EXTRA_TITLE));
         }
-        mWebView.loadUrl(String.valueOf(mIntent.getData()));
+        Uri uri = mIntent.getData();
+
+        if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
+            // HTMLViewer does not understand file provider content scheme,
+            // so convert into its equivalent file scheme
+            String path = uri.getPath();
+            final File file = new File(path);
+            if (isFileValid(file)) {
+                // Handle file scheme
+                mWebView.loadUrl(ContentResolver.SCHEME_FILE + "://" + path);
+                return;
+            }
+        }
+
+        mWebView.loadUrl(String.valueOf(uri));
     }
 
     private void setBackButton() {
@@ -96,6 +121,10 @@
         }
     }
 
+    private boolean isFileValid(final File file) {
+        return file.exists() && file.length() != 0;
+    }
+
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         if (item.getItemId() == android.R.id.home) {