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

Commit 34e3e3a0 authored by Raj Yengisetty's avatar Raj Yengisetty Committed by Gerrit Code Review
Browse files

DeskClock: Change action intent back to GET_CONTENT

- This this needed as a CMCC requirement to show the CMFM
and Sound Recorder as options in the DocumentsUI drawer
when the user chooses to set a Music file as their alarm
ringtone
- MANAGE_DOCUMENTS permission is requirement to retain access
to the URI returned by DocumentsUI when the user picks a file
from DocumentsUI internal storage
- The platform signature is required for MANAGE_DOCUMENTS

Revert "deskclock: take persistent grants of external uris"

Change-Id: Idd970801682b73b3f9e66d6651dcc80d208c05f2
parent d5641b50
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ LOCAL_STATIC_JAVA_LIBRARIES += android-opt-datetimepicker
LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_PACKAGE_NAME := DeskClock
LOCAL_CERTIFICATE := platform

LOCAL_OVERRIDES_PACKAGES := AlarmClock

+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
    <uses-permission android:name="com.android.deskclock.worldclock.permissions.WRITE_DATABASE" />

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21" ></uses-sdk>
+1 −82
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.UriPermission;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.ContentObserver;
@@ -97,7 +96,6 @@ import java.io.File;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -558,7 +556,7 @@ public class AlarmClockFragment extends DeskClockFragment implements
                    android.R.style.Theme_Material_Dialog);
            AlarmClockFragment.this.startActivityForResult(intent, REQUEST_CODE_RINGTONE);
        } else {
            final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                    AlarmClockFragment.this.mSelectedAlarm.alert);
            intent.setType(SEL_AUDIO_SRC);
@@ -642,81 +640,17 @@ public class AlarmClockFragment extends DeskClockFragment implements
        startActivityForResult(intent, REQUEST_CODE_PROFILE);
    }

    private void releaseRingtoneUri(Uri uri) {
        final ContentResolver cr = getActivity().getContentResolver();
        if (uri == null || mAdapter == null) {
            return;
        }

        // Check that the uri is currently persisted
        boolean containsUri = false;
        for (UriPermission uriPermission : cr.getPersistedUriPermissions()) {
            if (uriPermission.getUri().compareTo(uri) == 0) {
                containsUri = true;
                break;
            }
        }
        if (!containsUri) {
            return;
        }

        // Check that only one uri is in use
        int found = 0;
        int count = mAdapter.getCount();
        for (int i = 0; i < count; i++) {
            Alarm alarm = new Alarm((Cursor) mAdapter.getItem(i));
            if (alarm.alert != null && uri.compareTo(alarm.alert) == 0) {
                found++;
                if (found > 1) {
                    break;
                }
            }
        }
        if (found == 1) {
            // Release current granted uri
            try {
                if (uri != null) {
                    getActivity().getContentResolver().releasePersistableUriPermission(
                            uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            } catch (SecurityException e) {
                // Ignore
            }
        }
    }

    private Uri getRingtoneUri(Intent intent) {
        boolean releaseRingtoneUri = true;

        Uri uri;
        if (mSelectSource == SEL_SRC_RINGTONE) {
            uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        } else {
            uri = intent.getData();
            if (uri != null) {
                // if the alarms are the same, don't revoke the permissions
                releaseRingtoneUri = !uri.equals(mSelectedAlarm.alert);

                try {
                    getActivity().getContentResolver().takePersistableUriPermission(
                            uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } catch (SecurityException ex) {
                    LogUtils.e("Unable to take persistent grant permission for uri " + uri, ex);
                    uri = null;
                    final int msgId = R.string.take_persistent_grant_uri_permission_failed_msg;
                    Toast.makeText(getActivity(), msgId, Toast.LENGTH_SHORT).show();
                }
            }
        }
        if (uri == null) {
            uri = Alarm.NO_RINGTONE_URI;
        }

        if (releaseRingtoneUri) {
            // Release the current ringtone uri
            releaseRingtoneUri(mSelectedAlarm.alert);
        }

        return uri;
    }

@@ -1783,9 +1717,6 @@ public class AlarmClockFragment extends DeskClockFragment implements
            protected Void doInBackground(Void... parameters) {
                // Activity may be closed at this point , make sure data is still valid
                if (context != null && alarm != null) {
                    // Release the alarm ringtone uri
                    releaseRingtoneUri(alarm.alert);

                    ContentResolver cr = context.getContentResolver();
                    AlarmStateManager.deleteAllInstances(context, alarm.id);
                    Alarm.deleteAlarm(cr, alarm.id);
@@ -1841,18 +1772,6 @@ public class AlarmClockFragment extends DeskClockFragment implements
                    protected AlarmInstance doInBackground(Void ... parameters) {
                        ContentResolver cr = context.getContentResolver();

                        // Dismiss all old instances
                        AlarmStateManager.deleteAllInstances(context, alarm.id);
                        // Register/Update the ringtone uri
                        if (alarm.alert != null) {
                            try {
                                getActivity().getContentResolver().takePersistableUriPermission(
                                        alarm.alert, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            } catch (SecurityException ex) {
                                // Ignore
                            }
                        }

                        // Dismiss all old instances
                        AlarmStateManager.deleteAllInstances(context, alarm.id);