diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 625f0e9..34a4161 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -95,6 +95,7 @@ import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.browser.BrowserPresenter; import acr.browser.lightning.browser.BrowserView; +import acr.browser.lightning.browser.SearchBoxModel; import acr.browser.lightning.browser.TabsView; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.DownloadsPage; @@ -189,6 +190,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @Inject LightningDialogBuilder mBookmarksDialogBuilder; + @Inject SearchBoxModel mSearchBoxModel; + private TabsManager mTabsManager; // Image @@ -510,7 +513,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements final LightningView currentView = mTabsManager.getCurrentTab(); if (!hasFocus && currentView != null) { setIsLoading(currentView.getProgress() < 100); - updateUrl(currentView.getUrl(), true); + updateUrl(currentView.getUrl(), false); } else if (hasFocus && currentView != null) { // Hack to make sure the text gets selected @@ -1112,7 +1115,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } @Override - public void showBlockedLocalFileDialog(DialogInterface.OnClickListener listener) { + public void showBlockedLocalFileDialog(@NonNull DialogInterface.OnClickListener listener) { AlertDialog.Builder builder = new AlertDialog.Builder(this); Dialog dialog = builder.setCancelable(true) .setTitle(R.string.title_warning) @@ -1534,36 +1537,16 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } @Override - public void updateUrl(@Nullable String url, boolean shortUrl) { + public void updateUrl(@Nullable String url, boolean isLoading) { if (url == null || mSearch == null || mSearch.hasFocus()) { return; } final LightningView currentTab = mTabsManager.getCurrentTab(); mBookmarksView.handleUpdatedUrl(url); - if (shortUrl && !UrlUtils.isSpecialUrl(url)) { - switch (mPreferences.getUrlBoxContentChoice()) { - case 0: // Default, show only the domain - url = url.replaceFirst(Constants.HTTP, ""); - url = Utils.getDomainName(url); - mSearch.setText(url); - break; - case 1: // URL, show the entire URL - mSearch.setText(url); - break; - case 2: // Title, show the page's title - if (currentTab != null && !currentTab.getTitle().isEmpty()) { - mSearch.setText(currentTab.getTitle()); - } else { - mSearch.setText(mUntitledTitle); - } - break; - } - } else { - if (UrlUtils.isSpecialUrl(url)) { - url = ""; - } - mSearch.setText(url); - } + + String currentTitle = currentTab != null ? currentTab.getTitle() : null; + + mSearch.setText(mSearchBoxModel.getDisplayContent(url, currentTitle, isLoading)); } @Override diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index c47e3f4..4b74766 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -8,11 +8,11 @@ import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.activity.ThemableBrowserActivity; import acr.browser.lightning.activity.ThemableSettingsActivity; import acr.browser.lightning.browser.BrowserPresenter; +import acr.browser.lightning.browser.SearchBoxModel; import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.DownloadsPage; import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.StartPage; -import acr.browser.lightning.database.history.HistoryDatabase; import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.download.DownloadHandler; import acr.browser.lightning.download.LightningDownloadListener; @@ -83,6 +83,6 @@ public interface AppComponent { void inject(DownloadHandler downloadHandler); - HistoryDatabase historyDatabase(); + void inject(SearchBoxModel searchBoxModel); } diff --git a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java index 691d742..c53258c 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java @@ -121,7 +121,7 @@ public class BrowserPresenter { mView.updateProgress(newTab.getProgress()); mView.setBackButtonEnabled(newTab.canGoBack()); mView.setForwardButtonEnabled(newTab.canGoForward()); - mView.updateUrl(newTab.getUrl(), true); + mView.updateUrl(newTab.getUrl(), false); mView.setTabView(newTab.getWebView()); int index = mTabsModel.indexOfTab(newTab); if (index >= 0) { diff --git a/app/src/main/java/acr/browser/lightning/browser/BrowserView.java b/app/src/main/java/acr/browser/lightning/browser/BrowserView.java index b701bbb..e301a0c 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserView.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserView.java @@ -2,6 +2,7 @@ package acr.browser.lightning.browser; import android.content.DialogInterface; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.view.View; @@ -11,7 +12,7 @@ public interface BrowserView { void removeTabView(); - void updateUrl(String url, boolean shortUrl); + void updateUrl(@Nullable String url, boolean isLoading); void updateProgress(int progress); @@ -21,7 +22,7 @@ public interface BrowserView { void closeActivity(); - void showBlockedLocalFileDialog(DialogInterface.OnClickListener listener); + void showBlockedLocalFileDialog(@NonNull DialogInterface.OnClickListener listener); void showSnackbar(@StringRes int resource); diff --git a/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java b/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java new file mode 100644 index 0000000..c426677 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java @@ -0,0 +1,71 @@ +package acr.browser.lightning.browser; + +import android.app.Application; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.text.TextUtils; + +import javax.inject.Inject; + +import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.preference.PreferenceManager; +import acr.browser.lightning.utils.UrlUtils; +import acr.browser.lightning.utils.Utils; + +/** + * A UI model for the search box. + */ +public class SearchBoxModel { + + @Inject PreferenceManager mPreferences; + @Inject Application mApplication; + + @NonNull private final String mUntitledTitle; + + @Inject + public SearchBoxModel() { + BrowserApp.getAppComponent().inject(this); + mUntitledTitle = mApplication.getString(R.string.untitled); + } + + /** + * Returns the contents of the search box based on a variety of factors. + *