Search in sources :

Example 26 with ActionMode

use of android.support.v7.view.ActionMode in project Osmand by osmandapp.

the class FavoritesTreeFragment method deleteFavoritesAction.

private void deleteFavoritesAction() {
    int size = getSelectedFavoritesCount();
    if (groupsToDelete.size() + size > 0) {
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
        b.setMessage(getString(R.string.favorite_delete_multiple, size, groupsToDelete.size()));
        b.setPositiveButton(R.string.shared_string_delete, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (actionMode != null) {
                    actionMode.finish();
                }
                deleteFavorites();
            }
        });
        b.setNegativeButton(R.string.shared_string_cancel, null);
        b.show();
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) FavouritePoint(net.osmand.data.FavouritePoint)

Example 27 with ActionMode

use of android.support.v7.view.ActionMode in project android_packages_apps_Updater by LineageOS.

the class UpdatesListAdapter method startActionMode.

private void startActionMode(final UpdateInfo update, final boolean canDelete) {
    if (mActionMode != null) {
        Log.d(TAG, "Action mode already enabled");
        return;
    }
    mSelectedDownload = update.getDownloadId();
    notifyItemChanged(update.getDownloadId());
    // Hide Action Bar not to steal the focus when using a D-pad
    final boolean showActionBar;
    if (mActivity.getSupportActionBar() != null && mActivity.getSupportActionBar().isShowing()) {
        showActionBar = true;
        mActivity.getSupportActionBar().hide();
    } else {
        showActionBar = false;
    }
    mActionMode = mActivity.startSupportActionMode(new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.menu_action_mode, menu);
            menu.findItem(R.id.menu_delete_action).setVisible(canDelete);
            menu.findItem(R.id.menu_copy_url).setVisible(update.getAvailableOnline());
            menu.findItem(R.id.menu_export_update).setVisible(update.getPersistentStatus() == UpdateStatus.Persistent.VERIFIED);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()) {
                case R.id.menu_delete_action:
                    getDeleteDialog(update.getDownloadId()).setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            mode.finish();
                        }
                    }).show();
                    return true;
                case R.id.menu_copy_url:
                    Utils.addToClipboard(mActivity, mActivity.getString(R.string.label_download_url), update.getDownloadUrl(), mActivity.getString(R.string.toast_download_url_copied));
                    mode.finish();
                    return true;
                case R.id.menu_export_update:
                    // TODO: start exporting once the permission has been granted
                    boolean hasPermission = PermissionsUtils.checkAndRequestStoragePermission(mActivity, 0);
                    if (hasPermission) {
                        exportUpdate(update);
                    }
                    return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mSelectedDownload = null;
            mActionMode = null;
            notifyItemChanged(update.getDownloadId());
            if (showActionBar) {
                mActivity.getSupportActionBar().show();
            }
        }
    });
}
Also used : MenuInflater(android.view.MenuInflater) DialogInterface(android.content.DialogInterface) ActionMode(android.support.v7.view.ActionMode) MenuItem(android.view.MenuItem) Menu(android.view.Menu)

Example 28 with ActionMode

use of android.support.v7.view.ActionMode in project orgzly-android by orgzly.

the class NoteListFragment method onSaveInstanceState.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    /* Selection could be null if fragment is in back stack and there's configuration change. */
    if (mSelection != null) {
        mSelection.saveIds(outState);
    }
    /* Save action mode state (move mode). */
    if (mActionModeListener != null) {
        ActionMode actionMode = mActionModeListener.getActionMode();
        outState.putBoolean("actionModeMove", actionMode != null && "M".equals(actionMode.getTag()));
    }
}
Also used : ActionMode(android.support.v7.view.ActionMode)

Example 29 with ActionMode

use of android.support.v7.view.ActionMode in project actor-platform by actorapp.

the class MessagesDefaultFragment method onLongClick.

@Override
public boolean onLongClick(final Message message, final boolean hasMyReaction) {
    if (actionMode == null) {
        messagesAdapter.clearSelection();
        messagesAdapter.setSelected(message, true);
        actionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(new ActionMode.Callback() {

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                getActivity().getMenuInflater().inflate(R.menu.messages_context, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                Message[] selected = messagesAdapter.getSelected();
                if (selected.length > 0) {
                    actionMode.setTitle("" + selected.length);
                }
                boolean isAllText = true;
                for (Message k : selected) {
                    if (!(k.getContent() instanceof TextContent)) {
                        isAllText = false;
                        break;
                    }
                }
                menu.findItem(R.id.copy).setVisible(isAllText);
                menu.findItem(R.id.quote).setVisible(isAllText);
                menu.findItem(R.id.forward).setVisible(selected.length == 1 || isAllText);
                menu.findItem(R.id.like).setVisible(selected.length == 1);
                return false;
            }

            @Override
            public boolean onActionItemClicked(final ActionMode actionMode, MenuItem menuItem) {
                if (menuItem.getItemId() == R.id.delete) {
                    Message[] selected = messagesAdapter.getSelected();
                    final long[] rids = new long[selected.length];
                    for (int i = 0; i < rids.length; i++) {
                        rids[i] = selected[i].getRid();
                    }
                    new AlertDialog.Builder(getActivity()).setMessage(getString(R.string.alert_delete_messages_text).replace("{0}", "" + rids.length)).setPositiveButton(R.string.alert_delete_messages_yes, (dialog, which) -> {
                        messenger().deleteMessages(peer, rids);
                        actionMode.finish();
                    }).setNegativeButton(R.string.dialog_cancel, null).show().setCanceledOnTouchOutside(true);
                    return true;
                } else if (menuItem.getItemId() == R.id.copy) {
                    String text = messenger().getFormatter().formatMessagesExport(messagesAdapter.getSelected());
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    android.content.ClipData clip = android.content.ClipData.newPlainText("Messages", text);
                    clipboard.setPrimaryClip(clip);
                    Toast.makeText(getActivity(), R.string.toast_messages_copied, Toast.LENGTH_SHORT).show();
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.like) {
                    Message currentMessage = messagesAdapter.getSelected()[0];
                    if (hasMyReaction) {
                        ActorSDK.sharedActor().getMessenger().removeReaction(getPeer(), currentMessage.getRid(), "\u2764").start(new CommandCallback<Void>() {

                            @Override
                            public void onResult(Void res) {
                            }

                            @Override
                            public void onError(Exception e) {
                            }
                        });
                    } else {
                        ActorSDK.sharedActor().getMessenger().addReaction(getPeer(), currentMessage.getRid(), "\u2764").start(new CommandCallback<Void>() {

                            @Override
                            public void onResult(Void res) {
                            }

                            @Override
                            public void onError(Exception e) {
                            }
                        });
                    }
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.quote) {
                    String rawQuote = "";
                    int i = 0;
                    for (Message m : messagesAdapter.getSelected()) {
                        if (m.getContent() instanceof TextContent) {
                            UserVM user = users().get(m.getSenderId());
                            String nick = user.getNick().get();
                            String name = (nick != null && !nick.isEmpty()) ? "@" + nick : user.getName().get();
                            String text = ((TextContent) m.getContent()).getText();
                            rawQuote = rawQuote + name + ": " + text + "\n";
                        }
                    }
                    Fragment fragment = getParentFragment();
                    if (fragment instanceof MessagesFragmentCallback) {
                        ((MessagesFragmentCallback) fragment).onMessageQuote(rawQuote);
                    }
                    actionMode.finish();
                    return true;
                } else if (menuItem.getItemId() == R.id.forward) {
                    Intent i = new Intent(getActivity(), ShareActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    if (messagesAdapter.getSelected().length == 1) {
                        Message m = messagesAdapter.getSelected()[0];
                        if (m.getContent() instanceof TextContent) {
                            UserVM user = users().get(m.getSenderId());
                            String nick = user.getNick().get();
                            String name = (nick != null && !nick.isEmpty()) ? "@".concat(nick) : user.getName().get();
                            String text = ((TextContent) m.getContent()).getText();
                            String forward = name.concat(": ").concat(text).concat("\n");
                            i.putExtra(Intents.EXTRA_FORWARD_TEXT, forward);
                            i.putExtra(Intents.EXTRA_FORWARD_TEXT_RAW, forward);
                        } else if (!(m.getContent() instanceof UnsupportedContent)) {
                            AbsContent fileMessage = m.getContent();
                            try {
                                i.putExtra(Intents.EXTRA_FORWARD_CONTENT, AbsContent.serialize(fileMessage));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        String quote = "";
                        String rawQuote = "";
                        int j = 0;
                        for (Message m : messagesAdapter.getSelected()) {
                            if (m.getContent() instanceof TextContent) {
                                UserVM user = users().get(m.getSenderId());
                                String nick = user.getNick().get();
                                String name = (nick != null && !nick.isEmpty()) ? "@".concat(nick) : user.getName().get();
                                String text = ((TextContent) m.getContent()).getText();
                                quote = quote.concat(name).concat(": ").concat(text);
                                rawQuote = rawQuote.concat(name).concat(": ").concat(text).concat("\n");
                                if (j++ != messagesAdapter.getSelectedCount() - 1) {
                                    quote += ";\n";
                                } else {
                                    quote += "\n";
                                }
                            }
                        }
                        i.putExtra(Intents.EXTRA_FORWARD_TEXT, quote);
                        i.putExtra(Intents.EXTRA_FORWARD_TEXT_RAW, rawQuote);
                    }
                    actionMode.finish();
                    startActivity(i);
                    getActivity().finish();
                    return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {
                MessagesDefaultFragment.this.actionMode = null;
                messagesAdapter.clearSelection();
            }
        });
    } else {
        if (messagesAdapter.isSelected(message)) {
            messagesAdapter.setSelected(message, false);
            if (messagesAdapter.getSelectedCount() == 0) {
                actionMode.finish();
                actionMode = null;
            } else {
                actionMode.invalidate();
            }
        } else {
            messagesAdapter.setSelected(message, true);
            actionMode.invalidate();
        }
    }
    return true;
}
Also used : AlertDialog(android.app.AlertDialog) Context(android.content.Context) Bundle(android.os.Bundle) ActorSDKMessenger.myUid(im.actor.sdk.util.ActorSDKMessenger.myUid) ActorSDKMessenger.messenger(im.actor.sdk.util.ActorSDKMessenger.messenger) Intent(android.content.Intent) MenuItem(android.view.MenuItem) Toast(android.widget.Toast) Menu(android.view.Menu) CommandCallback(im.actor.core.viewmodel.CommandCallback) TextContent(im.actor.core.entity.content.TextContent) Intents(im.actor.sdk.controllers.Intents) ActorSDK(im.actor.sdk.ActorSDK) AbsContent(im.actor.core.entity.content.AbsContent) ActorSDKLauncher(im.actor.sdk.ActorSDKLauncher) ActionMode(android.support.v7.view.ActionMode) Void(im.actor.runtime.actors.messages.Void) Fragment(android.support.v4.app.Fragment) ActorSDKMessenger.users(im.actor.sdk.util.ActorSDKMessenger.users) IOException(java.io.IOException) UnsupportedContent(im.actor.core.entity.content.UnsupportedContent) AppCompatActivity(android.support.v7.app.AppCompatActivity) AlertDialog(android.app.AlertDialog) UserVM(im.actor.core.viewmodel.UserVM) R(im.actor.sdk.R) ChatActivity(im.actor.sdk.controllers.conversation.ChatActivity) Peer(im.actor.core.entity.Peer) ShareActivity(im.actor.sdk.controllers.share.ShareActivity) Message(im.actor.core.entity.Message) Message(im.actor.core.entity.Message) UnsupportedContent(im.actor.core.entity.content.UnsupportedContent) Fragment(android.support.v4.app.Fragment) Menu(android.view.Menu) Void(im.actor.runtime.actors.messages.Void) AbsContent(im.actor.core.entity.content.AbsContent) MenuItem(android.view.MenuItem) Intent(android.content.Intent) IOException(java.io.IOException) IOException(java.io.IOException) UserVM(im.actor.core.viewmodel.UserVM) CommandCallback(im.actor.core.viewmodel.CommandCallback) ActionMode(android.support.v7.view.ActionMode) TextContent(im.actor.core.entity.content.TextContent)

Aggregations

ActionMode (android.support.v7.view.ActionMode)15 Menu (android.view.Menu)11 MenuItem (android.view.MenuItem)11 View (android.view.View)10 AlertDialog (android.support.v7.app.AlertDialog)8 DialogInterface (android.content.DialogInterface)6 Context (android.content.Context)5 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 RecyclerView (android.support.v7.widget.RecyclerView)5 Toolbar (android.support.v7.widget.Toolbar)5 ArrayList (java.util.ArrayList)5 Bundle (android.os.Bundle)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 SuppressLint (android.annotation.SuppressLint)3 Intent (android.content.Intent)3 FastAdapter (com.mikepenz.fastadapter.FastAdapter)3 ActionModeHelper (com.mikepenz.fastadapter_extensions.ActionModeHelper)3 MaterializeBuilder (com.mikepenz.materialize.MaterializeBuilder)3 AlertDialog (android.app.AlertDialog)2