Skip to content

Commit

Permalink
✨ Address bar functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ejaz4 committed Jan 2, 2022
1 parent 05f8a45 commit 5a01bf2
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 52 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

8 changes: 6 additions & 2 deletions .idea/misc.xml

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

6 changes: 6 additions & 0 deletions .idea/render.experimental.xml

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

6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ android {

dependencies {
def lifecycle_version = "2.2.0"
def ff_version = "95.0"
def gecko_version = "$ff_version.20211218203254"


implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
Expand All @@ -51,7 +54,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation "org.mozilla.geckoview:geckoview:95.0.20211218203254"
implementation "org.mozilla.geckoview:geckoview:$gecko_version"
implementation "org.mozilla.components:browser-icons:$ff_version.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/java/co/dothq/browser/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import co.dothq.browser.subactivities.AddressBar
class BrowserActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState);

this.initStatusbar();

ApplicationManager().init(applicationContext, this);
Expand All @@ -43,24 +42,25 @@ class BrowserActivity : AppCompatActivity() {
session.navigationDelegate = BrowserDelegates().createNavigationDelegate("main", this, applicationContext);
session.progressDelegate = BrowserDelegates().createProgressDelegate("main", this, applicationContext);

var addressBarLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data

session.loadUri(data?.getStringExtra("targetURI").toString())
}
}


val addressBar = findViewById<LinearLayout>(R.id.addressBarContainer);

addressBar.setOnClickListener {
val addressBarIntent = Intent(this, AddressBar::class.java);

addressBarIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

var launchAddressBar = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
if (data != null) {
session.loadUri(data.toString());
}
}
}

launchAddressBar.launch(addressBarIntent)
addressBarIntent.putExtra("currentURI", StorageManager().get(applicationContext, "currentUri", "appValues", "about:blank").toString())
addressBarLauncher.launch(addressBarIntent)
overridePendingTransition(0, 0);
}

session.loadUri("https://ddg.gg")
Expand Down
43 changes: 24 additions & 19 deletions app/src/main/java/co/dothq/browser/BrowserDelegates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package co.dothq.browser
import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import co.dothq.browser.managers.StorageManager
import org.mozilla.geckoview.AllowOrDeny
import org.mozilla.geckoview.GeckoResult
Expand Down Expand Up @@ -44,30 +46,33 @@ class BrowserDelegates {
public fun createNavigationDelegate(area: String, context: Context, applicationCtx: Context): GeckoSession.NavigationDelegate {
return object : GeckoSession.NavigationDelegate {

override fun onLoadRequest(session: GeckoSession, request: GeckoSession.NavigationDelegate.LoadRequest): GeckoResult<AllowOrDeny>? {
StorageManager().set(applicationCtx, "dot.current.uri", request.uri.toString(), "appValues")
val uri = request.uri;
var uriNoProtocol = uri

if (uri.startsWith("https://")) uriNoProtocol = uriNoProtocol.replace("https://", "");
if (uri.startsWith("http://")) uriNoProtocol = uriNoProtocol.replace("http://", "");

if (uriNoProtocol.startsWith("www.")) uriNoProtocol = uriNoProtocol.replace("www.", "");
override fun onLocationChange(session: GeckoSession, url: String?) {
super.onLocationChange(session, url)
StorageManager().set(
applicationCtx,
"currentUri",
url.toString(),
"appValues"
)

var uriSplitBySlash = uriNoProtocol.split("/");
var uriJustHostname = uriSplitBySlash[0];
var path = uriSplitBySlash.drop(1).joinToString("/");
val uri: Uri = url.toString().toUri();
val host = uri.host.toString();
val path = url.toString().replace("${uri.scheme}://${uri.host}", "");

if (area == "main") {
val activity: Activity = (context as Activity)

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

if (path != "") activity.findViewById<TextView>(R.id.addressBarPath).text = "/${path.toString()}";
if (path == "") activity.findViewById<TextView>(R.id.addressBarPath).text = path.toString()
}
return (GeckoResult.allow())
if (path != "/") activity.findViewById<TextView>(R.id.addressBarPath).text = path
if (path == "/") activity.findViewById<TextView>(R.id.addressBarPath).text = ""
}
}

override fun onLoadRequest(session: GeckoSession, request: GeckoSession.NavigationDelegate.LoadRequest): GeckoResult<AllowOrDeny>? {
return (GeckoResult.allow())
}
}
}
}
}
}
45 changes: 44 additions & 1 deletion app/src/main/java/co/dothq/browser/subactivities/AddressBar.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
package co.dothq.browser.subactivities

import androidx.appcompat.app.AppCompatActivity
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import co.dothq.browser.R
import co.dothq.browser.managers.StorageManager


class AddressBar : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_address_bar)
val uri :String = intent.getStringExtra("currentURI").toString();

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

editBox.requestFocus();
editBox.selectAll()

editBox.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
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
setResult(Activity.RESULT_OK, data);
finish()
overridePendingTransition(0, 0)
}
return@OnKeyListener true
}
false
})

}

override fun onBackPressed() {
super.onBackPressed()
overridePendingTransition(0, 0)
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/input_layout_normal.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?colorPrimaryVariant"/>
<stroke android:width="0dp" android:color="?colorPrimaryVariant" />
<stroke android:width="0dp" android:color="?colorSecondary" />
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/invisible_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/invisible_button_pressed"
android:state_pressed="true"/>
<item android:drawable="@drawable/invisible_button_normal"
android:state_enabled="false"/>
</selector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/invisible_button_normal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/blue" />
<corners android:radius="10dp"/>
</shape>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/invisible_button_pressed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?colorSecondaryVariant" />
<stroke android:width="0dp" android:color="?colorSecondaryVariant" />
<corners android:radius="10dp"/>
</shape>
53 changes: 53 additions & 0 deletions app/src/main/res/layout/activity_address_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,57 @@
android:layout_height="match_parent"
tools:context=".subactivities.AddressBar">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="?colorPrimary"
android:orientation="horizontal"
android:paddingStart="16sp"
android:paddingTop="5sp"
android:paddingEnd="16sp"
android:paddingBottom="5sp">

<LinearLayout
android:id="@+id/addressBarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/input_layout"
android:orientation="horizontal">

<EditText
android:id="@+id/urlEnterBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:hint="@string/SEARCH_OR_ENTER_URL_TEXT"
android:lines="1"
android:minLines="1"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textSize="14sp"></EditText>

</LinearLayout>

<LinearLayout
android:id="@+id/closeAddressBarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="11dp"
android:background="@drawable/invisible_button"
android:orientation="vertical"
android:padding="5sp">

<LinearLayout
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:alpha="0.9"
android:background="@drawable/tab_icon"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 5a01bf2

Please sign in to comment.