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

Commit 1a792576 authored by Patrick Scott's avatar Patrick Scott
Browse files

Path to add alarms from other apps.

Add a new permission for adding alarms remotely.  Also add a test to verify this
path works.

Change-Id: I8533e927b8fd7e8c955df6448348593c28d1241c
parent a1d2dcbf
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ LOCAL_PACKAGE_NAME := DeskClock

LOCAL_OVERRIDES_PACKAGES := AlarmClock

LOCAL_SDK_VERSION := current
LOCAL_SDK_VERSION := 8

include $(BUILD_PACKAGE)

+23 −2
Original line number Diff line number Diff line
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android.deskclock">
        package="com.android.deskclock"
        android:versionCode="200" android:versionName="2.0.0">

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>

    <original-package android:name="com.android.alarmclock" />
    <original-package android:name="com.android.deskclock" />

    <!-- This permission is for internal use only.  It is meant to protect the
         internal alarm provider from other apps.  It uses the signature
         protection which means that apps signed with the same certificate
         would be able to use the permission.  Please do not do that! -->
    <permission android:name="com.android.deskclock.permission.PROVIDER"
                android:protectionLevel="signature"
                android:label="@string/permission_provider"
                android:description="@string/permission_provider_desc" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
@@ -15,7 +27,16 @@
    <application android:label="@string/app_label"
                 android:icon="@drawable/ic_launcher_alarmclock">

        <provider android:name="AlarmProvider" android:authorities="com.android.deskclock" />
        <provider android:name="AlarmProvider"
                  android:exported="false"
                  android:multiprocess="false"
                  android:writePermission="com.android.deskclock.permission.PROVIDER"
                  android:readPermission="com.android.deskclock.permission.PROVIDER"
                  android:authorities="com.android.deskclock" />

        <provider android:name="ExternalAlarmProvider"
                  android:authorities="com.android.deskclock.alarmprovider"
                  android:multiprocess="false" />

        <activity android:name="DeskClock"
                android:label="@string/app_label"
+5 −0
Original line number Diff line number Diff line
@@ -255,6 +255,11 @@

    <string name="menu_item_dock_settings" msgid="9161438664257429372">Dock settings</string>

    <string name="permission_provider">Read and write alarms</string>

    <string name="permission_provider_desc">Allow the application to read and
      write alarms to the Clock</string>

</resources>

+82 −0
Original line number Diff line number Diff line
/*
 * 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.
 */

package com.android.deskclock;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

/**
 * Helper class for opening the database from multiple providers.  Also provides
 * some common functionality.
 */
class AlarmDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "alarms.db";
    private static final int DATABASE_VERSION = 5;

    public AlarmDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE alarms (" +
                   "_id INTEGER PRIMARY KEY," +
                   "hour INTEGER, " +
                   "minutes INTEGER, " +
                   "daysofweek INTEGER, " +
                   "alarmtime INTEGER, " +
                   "enabled INTEGER, " +
                   "vibrate INTEGER, " +
                   "message TEXT, " +
                   "alert TEXT);");

        // insert default alarms
        String insertMe = "INSERT INTO alarms " +
                "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, " +
                " message, alert) VALUES ";
        db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
        db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int currentVersion) {
        if (Log.LOGV) Log.v(
                "Upgrading alarms database from version " +
                oldVersion + " to " + currentVersion +
                ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS alarms");
        onCreate(db);
    }

    Uri commonInsert(ContentValues values) {
        SQLiteDatabase db = getWritableDatabase();
        long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
        if (rowId < 0) {
            throw new SQLException("Failed to insert row");
        }
        if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);

        return ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
    }
}
+3 −54
Original line number Diff line number Diff line
@@ -17,20 +17,18 @@
package com.android.deskclock;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class AlarmProvider extends ContentProvider {
    private SQLiteOpenHelper mOpenHelper;
    private AlarmDatabaseHelper mOpenHelper;

    private static final int ALARMS = 1;
    private static final int ALARMS_ID = 2;
@@ -42,52 +40,12 @@ public class AlarmProvider extends ContentProvider {
        sURLMatcher.addURI("com.android.deskclock", "alarm/#", ALARMS_ID);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "alarms.db";
        private static final int DATABASE_VERSION = 5;

        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE alarms (" +
                       "_id INTEGER PRIMARY KEY," +
                       "hour INTEGER, " +
                       "minutes INTEGER, " +
                       "daysofweek INTEGER, " +
                       "alarmtime INTEGER, " +
                       "enabled INTEGER, " +
                       "vibrate INTEGER, " +
                       "message TEXT, " +
                       "alert TEXT);");

            // insert default alarms
            String insertMe = "INSERT INTO alarms " +
                    "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " +
                    "VALUES ";
            db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
            db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
            if (Log.LOGV) Log.v(
                    "Upgrading alarms database from version " +
                    oldVersion + " to " + currentVersion +
                    ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS alarms");
            onCreate(db);
        }
    }

    public AlarmProvider() {
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        mOpenHelper = new AlarmDatabaseHelper(getContext());
        return true;
    }

@@ -166,16 +124,7 @@ public class AlarmProvider extends ContentProvider {
            throw new IllegalArgumentException("Cannot insert into URL: " + url);
        }

        ContentValues values = new ContentValues(initialValues);

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
        if (rowId < 0) {
            throw new SQLException("Failed to insert row into " + url);
        }
        if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);

        Uri newUrl = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
        Uri newUrl = mOpenHelper.commonInsert(initialValues);
        getContext().getContentResolver().notifyChange(newUrl, null);
        return newUrl;
    }
Loading