Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5bd667a3 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "CameraBrowser: Support for deleting pictures."

parents 0cd0e797 147717c2
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     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

          http://www.apache.org/licenses/LICENSE-2.0

     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.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/delete"
        android:title="@string/delete_item" />
</menu>
+10 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

<resources>
    <string name="app_label">Camera Browser</string>

    <!-- for object info -->
    <string name="name_label">Name: </string>
    <string name="size_label">Size: </string>
    <string name="thumb_width_label">Thumb Width: </string>
@@ -28,4 +30,12 @@
    <string name="created_label">Created: </string>
    <string name="modified_label">Modified: </string>
    <string name="keywords_label">Keywords: </string>

    <!-- menu items -->
    <string name="delete_item">Delete</string>

    <!-- toasts -->
    <string name="object_deleted_message">Object deleted</string>
    <string name="delete_failed_message">Could not delete object</string>

</resources>
+54 −9
Original line number Diff line number Diff line
@@ -26,12 +26,16 @@ import android.net.Uri;
import android.os.Bundle;
import android.provider.Mtp;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

 /**
 * A list view displaying all objects within a container (folder or storage unit).
@@ -40,7 +44,8 @@ public class ObjectBrowser extends ListActivity {

    private static final String TAG = "ObjectBrowser";

    private ListAdapter mAdapter;
    private Cursor mCursor;
    private ObjectCursorAdapter mAdapter;
    private int mDeviceID;
    private int mStorageID;
    private int mObjectID;
@@ -67,17 +72,16 @@ public class ObjectBrowser extends ListActivity {
        mObjectID = getIntent().getIntExtra("object", 0);
        if (mDeviceID != 0 && mStorageID != 0) {
            Cursor c;
            Uri uri;
            if (mObjectID == 0) {
                c = getContentResolver().query(
                        Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID),
                        OBJECT_COLUMNS, null, null, null);
                uri = Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID);
            } else {
                c = getContentResolver().query(
                        Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID),
                        OBJECT_COLUMNS, null, null, null);
                uri = Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID);
            }
            Log.d(TAG, "query returned " + c);
            Log.d(TAG, "query " + uri);
            c = getContentResolver().query(uri, OBJECT_COLUMNS, null, null, null);
            startManagingCursor(c);
            mCursor = c;

            // Map Cursor columns to views defined in simple_list_item_1.xml
            mAdapter = new ObjectCursorAdapter(this, c);
@@ -115,6 +119,47 @@ public class ObjectBrowser extends ListActivity {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.object_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        int position = mList.getSelectedItemPosition();
        MenuItem item = menu.findItem(R.id.delete);
        item.setEnabled(position != AdapterView.INVALID_POSITION);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:
                deleteSelected();
                return true;
        }
        return false;
    }

    private void deleteSelected() {
        int position = mList.getSelectedItemPosition();
        int rowID = (int)mAdapter.getItemId(position);
        Uri uri = Mtp.Object.getContentUri(mDeviceID, rowID);

        Log.d(TAG, "deleting " + uri);

        int result = getContentResolver().delete(uri, null, null);
        if (result > 0) {
            Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show();
            mCursor.requery();
        } else {
            Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show();
        }
    }

    private class ObjectCursorAdapter extends ResourceCursorAdapter {

        public ObjectCursorAdapter(Context context, Cursor c) {