Browse Source

Change dialog UI to be list based instead of button based to allow for more options

master
Anthony Restaino 8 years ago
parent
commit
b3ee8ca155
  1. 56
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 138
      app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java
  3. 216
      app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java
  4. 6
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

56
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -40,6 +40,7 @@ import android.support.v7.app.ActionBar; @@ -40,6 +40,7 @@ import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
@ -108,6 +109,7 @@ import acr.browser.lightning.controller.UIController; @@ -108,6 +109,7 @@ import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.fragment.BookmarksFragment;
import acr.browser.lightning.fragment.TabsFragment;
@ -815,22 +817,17 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -815,22 +817,17 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* for. It highlights the text entered.
*/
private void findInPage() {
final AlertDialog.Builder finder = new AlertDialog.Builder(this);
finder.setTitle(getResources().getString(R.string.action_find));
final EditText getHome = new EditText(this);
getHome.setHint(getResources().getString(R.string.search_hint));
finder.setView(getHome);
finder.setPositiveButton(getResources().getString(R.string.search_hint),
new DialogInterface.OnClickListener() {
BrowserDialog.showEditText(this,
R.string.action_find,
R.string.search_hint,
R.string.search_hint, new BrowserDialog.EditorListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String query = getHome.getText().toString();
if (!query.isEmpty())
showSearchInterfaceBar(query);
public void onClick(String text) {
if (!TextUtils.isEmpty(text)) {
showSearchInterfaceBar(text);
}
}
});
finder.show();
}
private void showSearchInterfaceBar(String text) {
@ -863,32 +860,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -863,32 +860,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (position < 0) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1);
adapter.add(this.getString(R.string.close_all_tabs));
adapter.add(this.getString(R.string.close_other_tabs));
adapter.add(this.getString(R.string.close_tab));
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
BrowserDialog.show(this,
new BrowserDialog.Item(R.string.close_all_tabs) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
public void onClick() {
closeBrowser();
break;
case 1:
}
},
new BrowserDialog.Item(R.string.close_other_tabs) {
@Override
public void onClick() {
mPresenter.closeAllOtherTabs();
break;
case 2:
deleteTab(position);
break;
default:
break;
}
},
new BrowserDialog.Item(R.string.close_tab) {
@Override
public void onClick() {
deleteTab(position);
}
});
builder.show();
}
@Override

138
app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java

@ -0,0 +1,138 @@ @@ -0,0 +1,138 @@
package acr.browser.lightning.dialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import acr.browser.lightning.R;
import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils;
/**
* Copyright 7/31/2016 Anthony Restaino
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
*/
public class BrowserDialog {
public interface Listener {
void onClick();
}
public interface EditorListener {
void onClick(String text);
}
public static abstract class Item {
private int mTitle;
public Item(@StringRes int title) {
mTitle = title;
}
@StringRes
private int getTitle() {
return mTitle;
}
public abstract void onClick();
}
public static void show(@NonNull Activity activity, @NonNull Item item, @Nullable Item... items) {
show(activity, null, item, items);
}
public static void show(@NonNull Activity activity, @StringRes int title, @NonNull Item item, @Nullable Item... items) {
show(activity, activity.getString(title), item, items);
}
public static void show(@NonNull Activity activity, @Nullable String title, @NonNull Item item, @Nullable Item... items) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
ArrayAdapter<String> adapter = new ArrayAdapter<>(activity,
android.R.layout.simple_list_item_1);
final List<Item> itemList = new ArrayList<>(1);
itemList.add(item);
if (items != null) {
itemList.addAll(Arrays.asList(items));
}
for (Item it : itemList) {
adapter.add(activity.getString(it.getTitle()));
}
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
itemList.get(which).onClick();
}
});
builder.show();
}
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);
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());
}
});
editorDialog.show();
}
}

216
app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java

@ -8,6 +8,7 @@ import android.content.DialogInterface; @@ -8,6 +8,7 @@ import android.content.DialogInterface;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@ -56,7 +57,7 @@ public class LightningDialogBuilder { @@ -56,7 +57,7 @@ public class LightningDialogBuilder {
* @param context used to show the dialog
* @param url the long pressed url
*/
public void showLongPressedDialogForBookmarkUrl(@NonNull final Context context, @NonNull final String url) {
public void showLongPressedDialogForBookmarkUrl(@NonNull final Activity context, @NonNull final String url) {
final HistoryItem item;
if (url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME)) {
// TODO hacky, make a better bookmark mechanism in the future
@ -80,35 +81,28 @@ public class LightningDialogBuilder { @@ -80,35 +81,28 @@ public class LightningDialogBuilder {
}
}
public void showLongPressedDialogForBookmarkUrl(@NonNull final Context context, @NonNull final HistoryItem item) {
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
public void showLongPressedDialogForBookmarkUrl(@NonNull final Activity context, @NonNull final HistoryItem item) {
BrowserDialog.show(context, R.string.action_bookmarks,
new BrowserDialog.Item(R.string.action_new_tab) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(item.getUrl()));
break;
case DialogInterface.BUTTON_NEGATIVE:
}
},
new BrowserDialog.Item(R.string.action_delete) {
@Override
public void onClick() {
if (mBookmarkManager.deleteBookmark(item)) {
mEventBus.post(new BookmarkEvents.Deleted(item));
}
break;
case DialogInterface.BUTTON_NEUTRAL:
showEditBookmarkDialog(context, item);
break;
}
},
new BrowserDialog.Item(R.string.action_edit) {
@Override
public void onClick() {
showEditBookmarkDialog(context, item);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_bookmarks)
.setMessage(R.string.dialog_bookmark)
.setCancelable(true)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.setNeutralButton(R.string.action_edit, dialogClickListener)
.show();
});
}
private void showEditBookmarkDialog(@NonNull final Context context, @NonNull final HistoryItem item) {
@ -146,77 +140,55 @@ public class LightningDialogBuilder { @@ -146,77 +140,55 @@ public class LightningDialogBuilder {
editBookmarkDialog.show();
}
public void showBookmarkFolderLongPressedDialog(@NonNull final Context context, @NonNull final HistoryItem item) {
// assert item.isFolder();
final DialogInterface.OnClickListener dialogClickListener =
new DialogInterface.OnClickListener() {
public void showBookmarkFolderLongPressedDialog(@NonNull final Activity context, @NonNull final HistoryItem item) {
BrowserDialog.show(context, R.string.action_folder,
new BrowserDialog.Item(R.string.action_rename) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
public void onClick() {
showRenameFolderDialog(context, item);
break;
case DialogInterface.BUTTON_NEGATIVE:
}
},
new BrowserDialog.Item(R.string.action_delete) {
@Override
public void onClick() {
mBookmarkManager.deleteFolder(item.getTitle());
// setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
mEventBus.post(new BookmarkEvents.Deleted(item));
break;
}
}
};
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_folder)
.setMessage(R.string.dialog_folder)
.setCancelable(true)
.setPositiveButton(R.string.action_rename, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.show();
});
}
private void showRenameFolderDialog(@NonNull final Context context, @NonNull final HistoryItem item) {
// assert item.isFolder();
final AlertDialog.Builder editFolderDialog = new AlertDialog.Builder(context);
editFolderDialog.setTitle(R.string.title_rename_folder);
final EditText getTitle = new EditText(context);
getTitle.setHint(R.string.hint_title);
getTitle.setText(item.getTitle());
getTitle.setSingleLine();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
int padding = Utils.dpToPx(10);
layout.setPadding(padding, padding, padding, padding);
layout.addView(getTitle);
editFolderDialog.setView(layout);
editFolderDialog.setPositiveButton(context.getString(R.string.action_ok),
new DialogInterface.OnClickListener() {
private void showRenameFolderDialog(@NonNull final Activity context, @NonNull final HistoryItem item) {
BrowserDialog.showEditText(context, R.string.title_rename_folder,
R.string.hint_title, item.getTitle(),
R.string.action_ok, new BrowserDialog.EditorListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
public void onClick(String text) {
if (!TextUtils.isEmpty(text)) {
final String oldTitle = item.getTitle();
final String newTitle = getTitle.getText().toString();
final HistoryItem editedItem = new HistoryItem();
editedItem.setTitle(newTitle);
editedItem.setUrl(Constants.FOLDER + newTitle);
editedItem.setTitle(text);
editedItem.setUrl(Constants.FOLDER + text);
editedItem.setFolder(item.getFolder());
editedItem.setIsFolder(true);
mBookmarkManager.renameFolder(oldTitle, newTitle);
mBookmarkManager.renameFolder(oldTitle, text);
mEventBus.post(new BookmarkEvents.BookmarkChanged(item, editedItem));
}
}
});
editFolderDialog.show();
}
public void showLongPressedHistoryLinkDialog(final Context context, @NonNull final String url) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void showLongPressedHistoryLinkDialog(final Activity context, @NonNull final String url) {
BrowserDialog.show(context, R.string.action_history,
new BrowserDialog.Item(R.string.action_new_tab) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
}
},
new BrowserDialog.Item(R.string.action_delete) {
@Override
public void onClick() {
BrowserApp.getIOThread().execute(new Runnable() {
@Override
public void run() {
@ -225,86 +197,62 @@ public class LightningDialogBuilder { @@ -225,86 +197,62 @@ public class LightningDialogBuilder {
mEventBus.post(new BrowserEvents.OpenHistoryInCurrentTab());
}
});
break;
case DialogInterface.BUTTON_NEUTRAL:
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
default:
break;
}
},
new BrowserDialog.Item(R.string.action_open) {
@Override
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.action_history)
.setMessage(R.string.dialog_history_long_press)
.setCancelable(true)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_delete, dialogClickListener)
.setNeutralButton(R.string.action_open, dialogClickListener)
.show();
});
}
// TODO There should be a way in which we do not need an activity reference to dowload a file
public void showLongPressImageDialog(@NonNull final Activity activity, @NonNull final String url,
@NonNull final String userAgent) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
BrowserDialog.show(activity, url.replace(Constants.HTTP, ""),
new BrowserDialog.Item(R.string.action_new_tab) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
}
},
new BrowserDialog.Item(R.string.action_open) {
@Override
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
case DialogInterface.BUTTON_NEUTRAL:
Utils.downloadFile(activity, mPreferenceManager, url, userAgent, "attachment");
break;
}
},
new BrowserDialog.Item(R.string.action_download) {
@Override
public void onClick() {
Utils.downloadFile(activity, mPreferenceManager, url, userAgent, "attachment");
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(url.replace(Constants.HTTP, ""))
.setCancelable(true)
.setMessage(R.string.dialog_image)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_open, dialogClickListener)
.setNeutralButton(R.string.action_download, dialogClickListener)
.show();
});
}
public void showLongPressLinkDialog(@NonNull final Context context, final String url) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void showLongPressLinkDialog(@NonNull final Activity context, final String url) {
BrowserDialog.show(context, url,
new BrowserDialog.Item(R.string.action_new_tab) {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInNewTab(url));
break;
case DialogInterface.BUTTON_NEGATIVE:
}
},
new BrowserDialog.Item(R.string.action_open) {
@Override
public void onClick() {
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(url));
break;
case DialogInterface.BUTTON_NEUTRAL:
}
},
new BrowserDialog.Item(R.string.action_copy) {
@Override
public void onClick() {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", url);
clipboard.setPrimaryClip(clip);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context); // dialog
builder.setTitle(url)
.setCancelable(true)
.setMessage(R.string.dialog_link)
.setPositiveButton(R.string.action_new_tab, dialogClickListener)
.setNegativeButton(R.string.action_open, dialogClickListener)
.setNeutralButton(R.string.action_copy, dialogClickListener)
.show();
});
}
}

6
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -49,11 +49,13 @@ import acr.browser.lightning.database.HistoryItem; @@ -49,11 +49,13 @@ import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.async.ImageDownloadTask;
import com.anthonycr.bonsai.Action;
import com.anthonycr.bonsai.Observable;
import com.anthonycr.bonsai.OnSubscribe;
import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.Subscriber;
import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.view.LightningView;
@ -325,9 +327,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -325,9 +327,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
private void handleLongPress(@NonNull final HistoryItem item) {
if (item.isFolder()) {
mBookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getContext(), item);
mBookmarksDialogBuilder.showBookmarkFolderLongPressedDialog(getActivity(), item);
} else {
mBookmarksDialogBuilder.showLongPressedDialogForBookmarkUrl(getContext(), item);
mBookmarksDialogBuilder.showLongPressedDialogForBookmarkUrl(getActivity(), item);
}
}

Loading…
Cancel
Save