Skip to content

Commit

Permalink
🚧 Contextual identity now shows on address bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejaz4 committed Jan 5, 2022
1 parent 10e528d commit f0859ae
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}

buildTypes {
Expand All @@ -32,7 +33,7 @@ android {
sourceSets {
main {
assets {
srcDirs 'src\\main\\assets', 'src\\main\\java\\co\\dothq\\browser\\config'
srcDirs 'src\\main\\assets', 'src\\main\\java\\co\\dothq\\browser\\config', 'src\\main\\java\\co\\dothq\\browser\\extensions\\search'
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/co/dothq/browser/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import co.dothq.browser.managers.ApplicationManager
import co.dothq.browser.managers.PreferencesManager
import co.dothq.browser.managers.StorageManager
Expand All @@ -35,11 +38,12 @@ class BrowserActivity : AppCompatActivity() {

if (appSetup == false) return;
val view = findViewById<GeckoView>(R.id.geckoview)
val session = GeckoSession()
val runtime = GeckoRuntime.getDefault(this)
val session = GeckoSession()

session.open(runtime)
view.setSession(session)

session.navigationDelegate = BrowserDelegates().createNavigationDelegate("main", this, applicationContext);
session.progressDelegate = BrowserDelegates().createProgressDelegate("main", this, applicationContext);

Expand All @@ -48,7 +52,17 @@ class BrowserActivity : AppCompatActivity() {
// There are no request codes
val data: Intent? = result.data

session.loadUri(data?.getStringExtra("targetURI").toString())
val url = data?.getStringExtra("targetURI")?.toUri();

val host = url?.host.toString();
val path = url.toString().replace("${url?.scheme}://${url?.host}", "");

findViewById<TextView>(R.id.addressBarDomain).text = host.toString();

if (path != "/") findViewById<TextView>(R.id.addressBarPath).text = path
if (path == "/") findViewById<TextView>(R.id.addressBarPath).text = ""

session.loadUri(url.toString())
}
}

Expand All @@ -57,6 +71,7 @@ class BrowserActivity : AppCompatActivity() {

addressBar.setOnClickListener {
val addressBarIntent = Intent(this, AddressBar::class.java);
val contextualIdentityIcon = findViewById<ImageView>(R.id.contextIdentityIcon);

addressBarIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
addressBarIntent.putExtra("currentURI", StorageManager().get(applicationContext, "currentUri", "appValues", "about:blank").toString())
Expand All @@ -75,6 +90,11 @@ class BrowserActivity : AppCompatActivity() {
}
}

override fun onBackPressed() {
super.onBackPressed()
finish()
}

fun initStatusbar() {
if (resources.getString(R.string.mode) == "Day") {
getWindow().getDecorView()
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/co/dothq/browser/BrowserDelegates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ class BrowserDelegates {

if (area == "main") {
val activity: Activity = (context as Activity)
val contextualIdIcon = activity.findViewById<ImageView>(R.id.contextIdentityIcon);
val contextualIdIcon = activity.findViewById<ImageView>(R.id.contextIdentityIcon)



if (securityInfo.isSecure) contextualIdIcon.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.ic_secure_filled))
if (!securityInfo.isSecure) contextualIdIcon.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.ic_unsecure_filled))
Toast.makeText(context, securityInfo.securityMode.toString(), Toast.LENGTH_LONG).show()

StorageManager().set(applicationCtx, "contextualIdentity", securityInfo.isSecure, "appValues");
}
}

Expand All @@ -45,6 +48,7 @@ class BrowserDelegates {

public fun createNavigationDelegate(area: String, context: Context, applicationCtx: Context): GeckoSession.NavigationDelegate {
return object : GeckoSession.NavigationDelegate {
var canGoBack = false

override fun onLocationChange(session: GeckoSession, url: String?) {
super.onLocationChange(session, url)
Expand All @@ -67,12 +71,22 @@ class BrowserDelegates {

if (path != "/") activity.findViewById<TextView>(R.id.addressBarPath).text = path
if (path == "/") activity.findViewById<TextView>(R.id.addressBarPath).text = ""

if (path == "about:blank") {
activity.findViewById<TextView>(R.id.addressBarDomain).text = ""
activity.findViewById<TextView>(R.id.addressBarPath).text = ""
}
}
}

override fun onLoadRequest(session: GeckoSession, request: GeckoSession.NavigationDelegate.LoadRequest): GeckoResult<AllowOrDeny>? {

val activity: Activity = (context as Activity)
val contextualIdIcon = activity.findViewById<ImageView>(R.id.contextIdentityIcon)
contextualIdIcon.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.ic_globe))
return (GeckoResult.allow())
}

}
}
}
2 changes: 0 additions & 2 deletions app/src/main/java/co/dothq/browser/managers/StorageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class StorageManager {

val type = typePreferences.getString(id, null);

Log.d("sex", "${type} (${id})")
if (type == "String") {
return preferences.getString(id, null) ?: defaultValue;
}
Expand All @@ -28,7 +27,6 @@ class StorageManager {
}

if (type == "Boolean") {
Log.d("sex", preferences.getBoolean(id, false).toString())
return preferences.getBoolean(id, false) ?: defaultValue;
}

Expand Down
65 changes: 62 additions & 3 deletions app/src/main/java/co/dothq/browser/subactivities/AddressBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import co.dothq.browser.R
import co.dothq.browser.managers.PreferencesManager
import co.dothq.browser.managers.StorageManager


Expand All @@ -22,26 +27,80 @@ class AddressBar : AppCompatActivity() {
setContentView(R.layout.activity_address_bar)
val uri :String = intent.getStringExtra("currentURI").toString();

val contextualIdentity = StorageManager().get(applicationContext, "contextualIdentity", "appValues", false);

val contextualIdentityIcon = findViewById<ImageView>(R.id.contextIdentityIcon);

contextualIdentityIcon.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_unsecure_filled));

if (contextualIdentity is Boolean) {
if (contextualIdentity) contextualIdentityIcon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_secure_filled))
if (!contextualIdentity) contextualIdentityIcon.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_unsecure_filled));
}

val editBox = findViewById<EditText>(R.id.urlEnterBox);
editBox.setText(uri);

editBox.requestFocus();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
editBox.selectAll()

editBox.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
var text = editBox.text.toString().trim();

val uriRegex = """^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$""".toRegex()

if (!uriRegex.matches(text)) {
if (uriRegex.matches("http://${text}")) {
text = "http://${text}"
contextualIdentityIcon.setImageDrawable(
ContextCompat.getDrawable(this@AddressBar, R.drawable.ic_globe));
} else {
text = "https://duckduckgo.com/?q=%s".replace("%s", text)
contextualIdentityIcon.setImageDrawable(
ContextCompat.getDrawable(this@AddressBar, R.drawable.ic_search));
}
} else {
contextualIdentityIcon.setImageDrawable(
ContextCompat.getDrawable(this@AddressBar, R.drawable.ic_globe));
}
}

override fun afterTextChanged(s: Editable?) {}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
})

editBox.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->

var text = editBox.text.toString().trim();

if (event.action == KeyEvent.ACTION_UP) {
val uriRegex = """^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$""".toRegex()

if (!uriRegex.matches(text)) {
if (uriRegex.matches("http://${text}")) {
text = "http://${text}"
} else {
text = "https://duckduckgo.com/?q=%s".replace("%s", text)
}
}
}

if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {

var text = editBox.text.toString();
if (text.trim() == "") {
setResult(Activity.RESULT_CANCELED)
finish()
overridePendingTransition(0, 0)
} else {

val data = Intent()
data.putExtra("targetURI", text.trim());
// more handling here

data.putExtra("targetURI", text);
setResult(Activity.RESULT_OK, data);
finish()
overridePendingTransition(0, 0)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/co/dothq/browser/util/defaultProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class defaultProfile {

// dot.newtab
pref("dot.newtab.enabled", true, context)
pref("dot.newtab.urls", "about:home", context) // accepts a single URL or urls split by |
pref("dot.newtab.urls", "about:home", context)
pref("dot.search.engine_id", "[email protected]", context);

}
}
27 changes: 27 additions & 0 deletions app/src/main/res/drawable/ic_globe.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?colorSecondary"
android:strokeLineCap="round"/>
<path
android:pathData="M2,12L22,12"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?colorSecondary"
android:strokeLineCap="round"/>
<path
android:pathData="M12,2a15.3,15.3 0,0 1,4 10,15.3 15.3,0 0,1 -4,10 15.3,15.3 0,0 1,-4 -10,15.3 15.3,0 0,1 4,-10z"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="?colorSecondary"
android:strokeLineCap="round"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<path
android:pathData="M13,6.5C13,2.9102 10.0899,0 6.5,0C2.9102,0 0,2.9102 0,6.5C0,10.0899 2.9102,13 6.5,13C7.8573,13 9.1173,12.584 10.1597,11.8726C10.2616,11.8031 10.399,11.8132 10.4862,11.9004L14.2929,15.7071C14.6834,16.0976 15.3166,16.0976 15.7071,15.7071C16.0976,15.3166 16.0976,14.6834 15.7071,14.2929L11.9004,10.4862C11.8132,10.399 11.8031,10.2616 11.8726,10.1597C12.584,9.1173 13,7.8573 13,6.5ZM11,6.5C11,4.0147 8.9853,2 6.5,2C4.0147,2 2,4.0147 2,6.5C2,8.9853 4.0147,11 6.5,11C8.9853,11 11,8.9853 11,6.5Z"
android:fillColor="?colorSecondary"
android:fillType="evenOdd"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_secure_filled.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
android:viewportHeight="16">
<path
android:pathData="M12.2499,5.0103C12.1123,4.9989 12,4.8881 12,4.75V4C12,1.7909 10.2091,0 8,0C5.7909,0 4,1.7909 4,4V4.75C4,4.8881 3.8877,4.9989 3.7501,5.0103C2.2102,5.1373 1,6.4273 1,8V13C1,14.6569 2.3431,16 4,16H12C13.6569,16 15,14.6569 15,13V8C15,6.4273 13.7898,5.1373 12.2499,5.0103ZM10,4C10,2.8954 9.1046,2 8,2C6.8954,2 6,2.8954 6,4V4.75C6,4.8881 6.1119,5 6.25,5H9.75C9.8881,5 10,4.8881 10,4.75V4Z"
android:fillColor="#000000"
android:fillColor="?colorSecondary"
android:fillType="evenOdd"/>
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/ic_unsecure_filled.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
android:strokeLineCap="round"/>
<path
android:pathData="M13.8764,5.6591C14.5615,6.2089 15,7.0533 15,8V13C15,14.6569 13.6569,16 12,16H4C3.8528,16 3.7081,15.9894 3.5666,15.9689L13.8764,5.6591ZM1,11.4645V8C1,6.4273 2.2102,5.1373 3.7501,5.0103C3.8877,4.9989 4,4.8881 4,4.75V4C4,1.7909 5.7909,0 8,0C9.2228,0 10.3174,0.5487 11.0512,1.4133L9.6274,2.8371C9.2645,2.3303 8.6708,2 8,2C6.8954,2 6,2.8954 6,4V4.75C6,4.8881 6.1119,5 6.25,5H7.4645L1,11.4645Z"
android:fillColor="#000000"
android:fillType="evenOdd"/>
android:fillColor="?colorSecondary"
/>
</vector>
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_address_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
android:background="@drawable/input_layout"
android:orientation="horizontal">

<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="0"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/contextIdentityIcon"
android:layout_width="14dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="0.8"
app:srcCompat="@drawable/ic_secure_filled" />
</LinearLayout>

<EditText
android:id="@+id/urlEnterBox"
android:layout_width="match_parent"
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="0.8"
app:srcCompat="@drawable/ic_secure_filled"
app:tint="?attr/colorOnSecondary" />
app:srcCompat="@drawable/ic_secure_filled" />
</LinearLayout>

<TextView
Expand All @@ -59,7 +58,6 @@
android:lines="1"
android:maxLines="1"
android:minLines="1"
android:text="hostname/"
android:textAlignment="viewStart"
android:textColor="?colorSecondary"
android:visibility="visible" />
Expand All @@ -76,7 +74,6 @@
android:maxLines="1"
android:minLines="1"
android:paddingEnd="5sp"
android:text="path"
android:textAlignment="viewStart"
android:textColor="?colorSecondary"
app:autoSizeTextType="none" />
Expand Down

0 comments on commit f0859ae

Please sign in to comment.