From 06069770cba7471cbbf63edc2ff575fd5feb3b69 Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Sat, 27 May 2017 11:46:19 -0400 Subject: [PATCH] Cleaning up dialogs throughout the app --- .../lightning/activity/ReadingActivity.java | 95 +++++++------- .../lightning/dialog/BrowserDialog.java | 48 +++---- .../fragment/GeneralSettingsFragment.java | 123 ++++++++---------- .../lightning/view/LightningWebClient.java | 6 +- .../main/res/layout/dialog_edit_bookmark.xml | 28 ++-- app/src/main/res/layout/dialog_edit_text.xml | 16 +++ ...nual_proxy.xml => dialog_manual_proxy.xml} | 18 +-- 7 files changed, 174 insertions(+), 160 deletions(-) create mode 100644 app/src/main/res/layout/dialog_edit_text.xml rename app/src/main/res/layout/{picker_manual_proxy.xml => dialog_manual_proxy.xml} (70%) diff --git a/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java b/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java index bf2e0df..0f228cf 100644 --- a/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java @@ -30,12 +30,14 @@ import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.dialog.BrowserDialog; import acr.browser.lightning.preference.PreferenceManager; + import com.anthonycr.bonsai.Schedulers; import com.anthonycr.bonsai.Single; import com.anthonycr.bonsai.SingleAction; import com.anthonycr.bonsai.SingleOnSubscribe; import com.anthonycr.bonsai.SingleSubscriber; import com.anthonycr.bonsai.Subscription; + import acr.browser.lightning.reading.HtmlFetcher; import acr.browser.lightning.reading.JResult; import acr.browser.lightning.utils.ThemeUtils; @@ -148,45 +150,45 @@ public class ReadingActivity extends AppCompatActivity { if (getSupportActionBar() != null) getSupportActionBar().setTitle(Utils.getDomainName(mUrl)); mPageLoaderSubscription = loadPage(mUrl).subscribeOn(Schedulers.worker()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onStart() { - mProgressDialog = new ProgressDialog(ReadingActivity.this); - mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); - mProgressDialog.setCancelable(false); - mProgressDialog.setIndeterminate(true); - mProgressDialog.setMessage(getString(R.string.loading)); - mProgressDialog.show(); - BrowserDialog.setDialogSize(ReadingActivity.this, mProgressDialog); - } + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onStart() { + mProgressDialog = new ProgressDialog(ReadingActivity.this); + mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + mProgressDialog.setCancelable(false); + mProgressDialog.setIndeterminate(true); + mProgressDialog.setMessage(getString(R.string.loading)); + mProgressDialog.show(); + BrowserDialog.setDialogSize(ReadingActivity.this, mProgressDialog); + } - @Override - public void onItem(@Nullable ReaderInfo item) { - if (item == null || item.getTitle().isEmpty() || item.getBody().isEmpty()) { - setText(getString(R.string.untitled), getString(R.string.loading_failed)); - } else { - setText(item.getTitle(), item.getBody()); - } + @Override + public void onItem(@Nullable ReaderInfo item) { + if (item == null || item.getTitle().isEmpty() || item.getBody().isEmpty()) { + setText(getString(R.string.untitled), getString(R.string.loading_failed)); + } else { + setText(item.getTitle(), item.getBody()); } + } - @Override - public void onError(@NonNull Throwable throwable) { - setText(getString(R.string.untitled), getString(R.string.loading_failed)); - if (mProgressDialog != null && mProgressDialog.isShowing()) { - mProgressDialog.dismiss(); - mProgressDialog = null; - } + @Override + public void onError(@NonNull Throwable throwable) { + setText(getString(R.string.untitled), getString(R.string.loading_failed)); + if (mProgressDialog != null && mProgressDialog.isShowing()) { + mProgressDialog.dismiss(); + mProgressDialog = null; } + } - @Override - public void onComplete() { - if (mProgressDialog != null && mProgressDialog.isShowing()) { - mProgressDialog.dismiss(); - mProgressDialog = null; - } + @Override + public void onComplete() { + if (mProgressDialog != null && mProgressDialog.isShowing()) { + mProgressDialog.dismiss(); + mProgressDialog = null; } - }); + } + }); return true; } @@ -287,9 +289,8 @@ public class ReadingActivity extends AppCompatActivity { finish(); break; case R.id.text_size_item: - AlertDialog.Builder builder = new AlertDialog.Builder(this); - LayoutInflater inflater = this.getLayoutInflater(); - View view = inflater.inflate(R.layout.seek_layout, null); + + View view = LayoutInflater.from(this).inflate(R.layout.seek_layout, null); final SeekBar bar = (SeekBar) view.findViewById(R.id.text_size_seekbar); bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @@ -309,18 +310,20 @@ public class ReadingActivity extends AppCompatActivity { }); bar.setMax(5); bar.setProgress(mTextSize); - builder.setView(view); - builder.setTitle(R.string.size); - builder.setPositiveButton(android.R.string.ok, new OnClickListener() { - @Override - public void onClick(DialogInterface arg0, int arg1) { - mTextSize = bar.getProgress(); - mBody.setTextSize(getTextSize(mTextSize)); - mPreferences.setReadingTextSize(bar.getProgress()); - } + AlertDialog.Builder builder = new AlertDialog.Builder(this) + .setView(view) + .setTitle(R.string.size) + .setPositiveButton(android.R.string.ok, new OnClickListener() { - }); + @Override + public void onClick(DialogInterface dialog, int arg1) { + mTextSize = bar.getProgress(); + mBody.setTextSize(getTextSize(mTextSize)); + mPreferences.setReadingTextSize(bar.getProgress()); + } + + }); Dialog dialog = builder.show(); BrowserDialog.setDialogSize(this, dialog); break; diff --git a/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java b/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java index 3d5da19..9d3b964 100644 --- a/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java +++ b/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java @@ -123,37 +123,39 @@ public class BrowserDialog { }); } - public static void showEditText(@NonNull Activity activity, @StringRes int title, - @StringRes int hint, @StringRes int action, + public static void showEditText(@NonNull Activity activity, + @StringRes int title, + @StringRes int hint, + @StringRes int action, @NonNull final EditorListener listener) { showEditText(activity, title, hint, null, action, listener); } - public static void showEditText(@NonNull Activity activity, @StringRes int title, - @StringRes int hint, @Nullable String currentText, - @StringRes int action, @NonNull final EditorListener listener) { - final AlertDialog.Builder editorDialog = new AlertDialog.Builder(activity); - editorDialog.setTitle(title); - final EditText editText = new EditText(activity); + public static void showEditText(@NonNull Activity activity, + @StringRes int title, + @StringRes int hint, + @Nullable String currentText, + @StringRes int action, + @NonNull final EditorListener listener) { + View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_edit_text, null); + final EditText editText = (EditText) dialogView.findViewById(R.id.dialog_edit_text); + editText.setHint(hint); if (currentText != null) { editText.setText(currentText); } - editText.setSingleLine(); - LinearLayout layout = new LinearLayout(activity); - layout.setOrientation(LinearLayout.VERTICAL); - int padding = Utils.dpToPx(10); - layout.setPadding(padding, padding, padding, padding); - layout.addView(editText); - editorDialog.setView(layout); - editorDialog.setPositiveButton(action, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - listener.onClick(editText.getText().toString()); - } - }); + + final AlertDialog.Builder editorDialog = new AlertDialog.Builder(activity) + .setTitle(title) + .setView(dialogView) + .setPositiveButton(action, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + listener.onClick(editText.getText().toString()); + } + }); Dialog dialog = editorDialog.show(); setDialogSize(activity, dialog); diff --git a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java index da03e5d..b37fa55 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java @@ -17,10 +17,9 @@ import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; +import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.widget.EditText; -import android.widget.LinearLayout; import acr.browser.lightning.BuildConfig; import acr.browser.lightning.R; @@ -31,7 +30,7 @@ import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.Utils; -import static acr.browser.lightning.preference.PreferenceManager.*; +import static acr.browser.lightning.preference.PreferenceManager.Suggestion; public class GeneralSettingsFragment extends LightningPreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { @@ -163,24 +162,20 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } private void searchUrlPicker() { - final AlertDialog.Builder urlPicker = new AlertDialog.Builder(mActivity); - urlPicker.setTitle(getResources().getString(R.string.custom_url)); - final EditText getSearchUrl = new EditText(mActivity); - String mSearchUrl = mPreferenceManager.getSearchUrl(); - getSearchUrl.setText(mSearchUrl); - urlPicker.setView(getSearchUrl); - urlPicker.setPositiveButton(getResources().getString(R.string.action_ok), - new DialogInterface.OnClickListener() { + + BrowserDialog.showEditText(mActivity, + R.string.custom_url, + R.string.custom_url, + mPreferenceManager.getSearchUrl(), + R.string.action_ok, + new BrowserDialog.EditorListener() { @Override - public void onClick(DialogInterface dialog, int which) { - String text = getSearchUrl.getText().toString(); + public void onClick(String text) { mPreferenceManager.setSearchUrl(text); - searchengine.setSummary(getResources().getString(R.string.custom_url) + ": " - + text); + searchengine.setSummary(mActivity.getString(R.string.custom_url) + ": " + text); } }); - Dialog dialog = urlPicker.show(); - BrowserDialog.setDialogSize(mActivity, dialog); + } private void getFlashChoice() { @@ -217,7 +212,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme private void proxyChoicePicker() { AlertDialog.Builder picker = new AlertDialog.Builder(mActivity); - picker.setTitle(getResources().getString(R.string.http_proxy)); + picker.setTitle(R.string.http_proxy); picker.setSingleChoiceItems(mProxyChoices, mPreferenceManager.getProxyChoice(), new DialogInterface.OnClickListener() { @@ -226,7 +221,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme setProxyChoice(which); } }); - picker.setPositiveButton(getResources().getString(R.string.action_ok), null); + picker.setPositiveButton(R.string.action_ok, null); Dialog dialog = picker.show(); BrowserDialog.setDialogSize(mActivity, dialog); } @@ -252,7 +247,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } private void manualProxyPicker() { - View v = mActivity.getLayoutInflater().inflate(R.layout.picker_manual_proxy, null); + View v = mActivity.getLayoutInflater().inflate(R.layout.dialog_manual_proxy, null); final EditText eProxyHost = (EditText) v.findViewById(R.id.proxyHost); final EditText eProxyPort = (EditText) v.findViewById(R.id.proxyPort); @@ -405,28 +400,26 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } private void homePicker() { - final AlertDialog.Builder homePicker = new AlertDialog.Builder(mActivity); - homePicker.setTitle(getResources().getString(R.string.title_custom_homepage)); - final EditText getHome = new EditText(mActivity); + String currentHomepage; mHomepage = mPreferenceManager.getHomepage(); if (!mHomepage.startsWith(Constants.ABOUT)) { - getHome.setText(mHomepage); + currentHomepage = mHomepage; } else { - String defaultUrl = "https://www.google.com"; - getHome.setText(defaultUrl); + currentHomepage = "https://www.google.com"; } - homePicker.setView(getHome); - homePicker.setPositiveButton(getResources().getString(R.string.action_ok), - new DialogInterface.OnClickListener() { + + BrowserDialog.showEditText(mActivity, + R.string.title_custom_homepage, + R.string.title_custom_homepage, + currentHomepage, + R.string.action_ok, + new BrowserDialog.EditorListener() { @Override - public void onClick(DialogInterface dialog, int which) { - String text = getHome.getText().toString(); + public void onClick(String text) { mPreferenceManager.setHomepage(text); home.setSummary(text); } }); - Dialog dialog = homePicker.show(); - BrowserDialog.setDialogSize(mActivity, dialog); } private void downloadLocDialog() { @@ -492,49 +485,45 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } private void agentPicker() { - final AlertDialog.Builder agentStringPicker = new AlertDialog.Builder(mActivity); - agentStringPicker.setTitle(getResources().getString(R.string.title_user_agent)); - final EditText getAgent = new EditText(mActivity); - agentStringPicker.setView(getAgent); - agentStringPicker.setPositiveButton(getResources().getString(R.string.action_ok), - new DialogInterface.OnClickListener() { + + BrowserDialog.showEditText(mActivity, + R.string.title_user_agent, + R.string.title_user_agent, + mPreferenceManager.getUserAgentString(""), + R.string.action_ok, + new BrowserDialog.EditorListener() { @Override - public void onClick(DialogInterface dialog, int which) { - String text = getAgent.getText().toString(); + public void onClick(String text) { mPreferenceManager.setUserAgentString(text); - useragent.setSummary(getResources().getString(R.string.agent_custom)); + useragent.setSummary(mActivity.getString(R.string.agent_custom)); } }); - Dialog dialog = agentStringPicker.show(); - BrowserDialog.setDialogSize(mActivity, dialog); } private void downPicker() { - final AlertDialog.Builder downLocationPicker = new AlertDialog.Builder(mActivity); - LinearLayout layout = new LinearLayout(mActivity); - downLocationPicker.setTitle(getResources().getString(R.string.title_download_location)); - final EditText getDownload = new EditText(mActivity); - getDownload.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT)); - getDownload.setText(mPreferenceManager.getDownloadDirectory()); - final int errorColor = ContextCompat.getColor(getActivity(), R.color.error_red); - final int regularColor = ThemeUtils.getTextColor(getActivity()); + + View dialogView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_edit_text, null); + final EditText getDownload = (EditText) dialogView.findViewById(R.id.dialog_edit_text); + + final int errorColor = ContextCompat.getColor(mActivity, R.color.error_red); + final int regularColor = ThemeUtils.getTextColor(mActivity); getDownload.setTextColor(regularColor); getDownload.addTextChangedListener(new DownloadLocationTextWatcher(getDownload, errorColor, regularColor)); getDownload.setText(mPreferenceManager.getDownloadDirectory()); - layout.addView(getDownload); - downLocationPicker.setView(layout); - downLocationPicker.setPositiveButton(getResources().getString(R.string.action_ok), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String text = getDownload.getText().toString(); - text = DownloadHandler.addNecessarySlashes(text); - mPreferenceManager.setDownloadDirectory(text); - downloadloc.setSummary(text); - } - }); + AlertDialog.Builder downLocationPicker = new AlertDialog.Builder(mActivity) + .setTitle(R.string.title_download_location) + .setView(dialogView) + .setPositiveButton(R.string.action_ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String text = getDownload.getText().toString(); + text = DownloadHandler.addNecessarySlashes(text); + mPreferenceManager.setDownloadDirectory(text); + downloadloc.setSummary(text); + } + }); Dialog dialog = downLocationPicker.show(); BrowserDialog.setDialogSize(mActivity, dialog); } @@ -640,11 +629,11 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } private static class DownloadLocationTextWatcher implements TextWatcher { - private final EditText getDownload; + @NonNull private final EditText getDownload; private final int errorColor; private final int regularColor; - public DownloadLocationTextWatcher(EditText getDownload, int errorColor, int regularColor) { + public DownloadLocationTextWatcher(@NonNull EditText getDownload, int errorColor, int regularColor) { this.getDownload = getDownload; this.errorColor = errorColor; this.regularColor = regularColor; diff --git a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java index 5ec085a..0d1fd33 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java @@ -141,9 +141,9 @@ public class LightningWebClient extends WebViewClient { realmLabel.setText(mActivity.getString(R.string.label_realm, realm)); builder.setView(dialogView) - .setTitle(mActivity.getString(R.string.title_sign_in)) + .setTitle(R.string.title_sign_in) .setCancelable(true) - .setPositiveButton(mActivity.getString(R.string.title_sign_in), + .setPositiveButton(R.string.title_sign_in, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { @@ -153,7 +153,7 @@ public class LightningWebClient extends WebViewClient { Log.d(TAG, "Attempting HTTP Authentication"); } }) - .setNegativeButton(mActivity.getString(R.string.action_cancel), + .setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { diff --git a/app/src/main/res/layout/dialog_edit_bookmark.xml b/app/src/main/res/layout/dialog_edit_bookmark.xml index 7141e0a..19db5d2 100644 --- a/app/src/main/res/layout/dialog_edit_bookmark.xml +++ b/app/src/main/res/layout/dialog_edit_bookmark.xml @@ -1,29 +1,33 @@ - + + android:hint="@string/hint_title" + android:maxLines="1" + android:singleLine="true"/> + android:inputType="textUri" + android:maxLines="1" + android:singleLine="true"/> \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_edit_text.xml b/app/src/main/res/layout/dialog_edit_text.xml new file mode 100644 index 0000000..283c5a8 --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_text.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/picker_manual_proxy.xml b/app/src/main/res/layout/dialog_manual_proxy.xml similarity index 70% rename from app/src/main/res/layout/picker_manual_proxy.xml rename to app/src/main/res/layout/dialog_manual_proxy.xml index c59d030..cb7d32a 100644 --- a/app/src/main/res/layout/picker_manual_proxy.xml +++ b/app/src/main/res/layout/dialog_manual_proxy.xml @@ -1,12 +1,10 @@ - + android:padding="@dimen/dialog_padding"> + android:layout_marginRight="@dimen/default_padding" + android:text="@string/host"/> + android:inputType="text"/> + android:layout_marginRight="@dimen/default_padding" + android:text="@string/port"/> + android:inputType="number"/> \ No newline at end of file