Commit 71ff34ea authored by Xinayder's avatar Xinayder Committed by luca020400

Jelly: add dialog to show SSL certificate info

* Based off the same functionality from Soren Stoutner's Privacy Browser
  (https://git.stoutner.com/?p=PrivacyBrowser.git)

* Preview: https://goo.gl/DPYfUQ

Change-Id: I46d568ed794183f94f41dc5e5a4543df7917eb37
(cherry picked from commit 77b808bd)
parent a80ca961
/*
* Copyright (C) 2017 The LineageOS 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.
*/
package org.lineageos.jelly.ui;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.lineageos.jelly.R;
public class KeyValueView extends LinearLayout {
private TextView mKeyView;
private TextView mValueView;
public KeyValueView(Context context) {
super(context);
init();
}
public KeyValueView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public KeyValueView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
inflate(getContext(), R.layout.key_value_view, this);
mKeyView = (TextView) findViewById(R.id.key);
mValueView = (TextView) findViewById(R.id.value);
}
public void setText(@StringRes int attributeTextResId, String value) {
if (!value.isEmpty()) {
this.mKeyView.setText(attributeTextResId);
this.mValueView.setText(value);
} else {
setVisibility(View.GONE);
}
}
}
......@@ -15,9 +15,21 @@
*/
package org.lineageos.jelly.ui;
import android.app.AlertDialog;
import android.content.Context;
import android.net.Uri;
import android.net.http.SslCertificate;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.lineageos.jelly.R;
import java.text.DateFormat;
import java.util.Date;
public class UrlBarController implements View.OnFocusChangeListener {
private EditText mEditor;
......@@ -44,12 +56,13 @@ public class UrlBarController implements View.OnFocusChangeListener {
updateSecureIconVisibility();
}
public void onPageLoadFinished() {
public void onPageLoadFinished(Context context, SslCertificate certificate) {
mLoading = false;
if (!mUrlBarHasFocus) {
updateUrlBarText();
}
updateSecureIconVisibility();
updateSSLCertificateDialog(context, certificate);
}
public void onTitleReceived(String title) {
......@@ -79,4 +92,59 @@ public class UrlBarController implements View.OnFocusChangeListener {
private boolean isSecure() {
return mUrl != null && mUrl.startsWith("https");
}
private void updateSSLCertificateDialog(Context context, SslCertificate certificate) {
if (certificate == null) return;
// Show the dialog if you tap the lock icon and the cert is valid
mSecureIcon.setOnClickListener((v) -> {
View view = LayoutInflater.from(context).
inflate(R.layout.dialog_ssl_certificate_info, new LinearLayout(context));
// Get the text views
TextView domainView = (TextView) view.findViewById(R.id.domain);
KeyValueView issuedToCNView = (KeyValueView) view.findViewById(R.id.issued_to_cn);
KeyValueView issuedToOView = (KeyValueView) view.findViewById(R.id.issued_to_o);
KeyValueView issuedToUNView = (KeyValueView) view.findViewById(R.id.issued_to_un);
KeyValueView issuedByCNView = (KeyValueView) view.findViewById(R.id.issued_by_cn);
KeyValueView issuedByOView = (KeyValueView) view.findViewById(R.id.issued_by_o);
KeyValueView issuedByUNView = (KeyValueView) view.findViewById(R.id.issued_by_un);
KeyValueView issuedOnView = (KeyValueView) view.findViewById(R.id.issued_on);
KeyValueView expiresOnView = (KeyValueView) view.findViewById(R.id.expires_on);
// Get the domain name
String domainString = Uri.parse(mUrl).getHost();
// Get the validity dates
Date startDate = certificate.getValidNotBeforeDate();
Date endDate = certificate.getValidNotAfterDate();
// Update TextViews
domainView.setText(domainString);
issuedToCNView.setText(R.string.ssl_cert_dialog_common_name,
certificate.getIssuedTo().getCName());
issuedToOView.setText(R.string.ssl_cert_dialog_organization,
certificate.getIssuedTo().getOName());
issuedToUNView.setText(R.string.ssl_cert_dialog_organizational_unit,
certificate.getIssuedTo().getUName());
issuedByCNView.setText(R.string.ssl_cert_dialog_common_name,
certificate.getIssuedBy().getCName());
issuedByOView.setText(R.string.ssl_cert_dialog_organization,
certificate.getIssuedBy().getOName());
issuedByUNView.setText(R.string.ssl_cert_dialog_organizational_unit,
certificate.getIssuedBy().getUName());
issuedOnView.setText(R.string.ssl_cert_dialog_issued_on,
DateFormat.getDateTimeInstance().format(startDate));
expiresOnView.setText(R.string.ssl_cert_dialog_expires_on,
DateFormat.getDateTimeInstance().format(endDate));
// Build and show the dialog
new AlertDialog.Builder(context)
.setTitle(R.string.ssl_cert_dialog_title)
.setView(view)
.setNegativeButton(R.string.ssl_cert_dialog_dismiss, null)
.create()
.show();
});
}
}
......@@ -67,7 +67,7 @@ class WebClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mUrlBarController.onPageLoadFinished();
mUrlBarController.onPageLoadFinished(view.getContext(), view.getCertificate());
}
@Override
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2017 The LineageOS 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.
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp">
<!-- Domain Name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/ssl_cert_dialog_domain"
android:textAllCaps="true"
android:textStyle="bold" />
<TextView
android:id="@+id/domain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent" />
<!-- Issued To -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/ssl_cert_dialog_issued_to"
android:textAllCaps="true"
android:textStyle="bold" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_to_cn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_to_o"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_to_un"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- Issued By -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/ssl_cert_dialog_issued_by"
android:textAllCaps="true"
android:textStyle="bold" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_by_cn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_by_o"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_by_un"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- Period of Validity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/ssl_cert_dialog_validity"
android:textAllCaps="true"
android:textStyle="bold" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/issued_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<org.lineageos.jelly.ui.KeyValueView
android:id="@+id/expires_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
</LinearLayout>
</ScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2017 The LineageOS 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/key"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent" />
</LinearLayout>
\ No newline at end of file
......@@ -174,4 +174,17 @@
<!-- Search in page: search hint -->
<string name="search_bar_hint">Search</string>
<!-- SSL Certificate Details dialog -->
<string name="ssl_cert_dialog_title">SSL Certificate Details</string>
<string name="ssl_cert_dialog_domain">Domain</string>
<string name="ssl_cert_dialog_issued_to">Issued to</string>
<string name="ssl_cert_dialog_issued_by">Issued by</string>
<string name="ssl_cert_dialog_validity">Validity Period</string>
<string name="ssl_cert_dialog_common_name">Common Name (CN)</string>
<string name="ssl_cert_dialog_organization">Organization (O)</string>
<string name="ssl_cert_dialog_organizational_unit">Organizational Unit (UN)</string>
<string name="ssl_cert_dialog_issued_on">Issued On</string>
<string name="ssl_cert_dialog_expires_on">Expires On</string>
<string name="ssl_cert_dialog_dismiss">Dismiss</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment