Solution to Challenge: Deleting Photos


#1

This is my solution to the challenge. For the first part, inside omActivityResult:

...
if (mCrime.getPhoto() != null) {
    getActivity().deleteFile(mCrime.getPhoto().getFilename());
    PictureUtils.cleanImageView(mPhotoView);
}
Photo p = new Photo(filename);
...

Second part follows the challenge from a previous chapter, ActionMode implementation for ABS. First setup the long click listener for the imageview:

mPhotoView.setLongClickable(true);
mPhotoView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if (!(mPhotoView.getDrawable() instanceof BitmapDrawable)) {
            return false;
        }
        v.setLongClickable(false);
        getActivity().startActionMode(new ActionModeListener(v)).setTitle("Delete?");
        return true;
    }
});

Then setup the ActionModeListener class:

public class ActionModeListener implements ActionMode.Callback {
    private View view;
    
    public ActionModeListener (View v) {
        view = v;
    }
    
    // The view type checked and recasted for class reuseability
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_delete_crime:
                if (view instanceof ImageView) {
                    ImageView imageView = (ImageView) view;
                    getActivity().deleteFile(mCrime.getPhoto().getFilename());
                    mCrime.setPhoto(null);
                    PictureUtils.cleanImageView(imageView);
                    imageView.invalidate();
                }
                mode.finish();
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.crime_list_item_context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        view.setLongClickable(true);			
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        return false;
    }
}

To do: somehow highlight the selected imageview. But how?