Search in sources :

Example 1 with AlarmInstance

use of org.omnirom.deskclock.provider.AlarmInstance in project android_packages_apps_OmniClock by omnirom.

the class AlarmService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LogUtils.v("AlarmService.onStartCommand() with intent: " + intent.toString());
    long instanceId = AlarmInstance.getId(intent.getData());
    if (AlarmConstants.START_ALARM_ACTION.equals(intent.getAction())) {
        ContentResolver cr = this.getContentResolver();
        AlarmInstance instance = AlarmInstance.getInstance(cr, instanceId);
        if (instance == null) {
            LogUtils.e("No instance found to start alarm: " + instanceId);
            if (mCurrentAlarm != null) {
                // Only release lock if we are not firing alarm
                AlarmAlertWakeLock.releaseCpuLock();
            }
            return Service.START_NOT_STICKY;
        } else if (mCurrentAlarm != null && mCurrentAlarm.mId == instanceId) {
            LogUtils.e("Alarm already started for instance: " + instanceId);
            return Service.START_NOT_STICKY;
        }
        // service foreground notification is not shown on wear
        if (Utils.showWearNotification(this)) {
            AlarmNotifications.showWearAlarmNotification(this, instance);
        }
        // MUST be different then the other notifications
        // else if killing service is after state change it
        // will take down our state notification e.g. snoozed
        startForeground(instance.hashCode() + 1, showAlarmNotification(this, instance));
        startAlarm(instance);
    } else if (AlarmConstants.STOP_ALARM_ACTION.equals(intent.getAction())) {
        if (mCurrentAlarm != null && mCurrentAlarm.mId != instanceId) {
            LogUtils.e("Can't stop alarm for instance: " + instanceId + " because current alarm is: " + mCurrentAlarm.mId);
            return Service.START_NOT_STICKY;
        }
        boolean fromDismiss = intent.getBooleanExtra(AlarmConstants.DATA_ALARM_EXTRA_DISMISSED, false);
        stopCurrentAlarm();
        // and must never stop service
        if (fromDismiss) {
            stopSelf();
        }
    }
    return Service.START_NOT_STICKY;
}
Also used : AlarmInstance(org.omnirom.deskclock.provider.AlarmInstance) ContentResolver(android.content.ContentResolver)

Example 2 with AlarmInstance

use of org.omnirom.deskclock.provider.AlarmInstance in project android_packages_apps_OmniClock by omnirom.

the class AlarmClockFragment method setupAlarmInstance.

private static AlarmInstance setupAlarmInstance(Context context, Alarm alarm) {
    ContentResolver cr = context.getContentResolver();
    AlarmInstance newInstance = alarm.createInstanceAfter(Calendar.getInstance());
    newInstance = AlarmInstance.addInstance(cr, newInstance);
    // Register instance to state manager
    AlarmStateManager.registerInstance(context, newInstance, true);
    return newInstance;
}
Also used : AlarmInstance(org.omnirom.deskclock.provider.AlarmInstance) ContentResolver(android.content.ContentResolver)

Example 3 with AlarmInstance

use of org.omnirom.deskclock.provider.AlarmInstance in project android_packages_apps_OmniClock by omnirom.

the class AlarmClockFragment method asyncAddAlarm.

private void asyncAddAlarm(final Alarm alarm, final boolean expand) {
    final Context context = getActivity().getApplicationContext();
    final AsyncTask<Void, Void, AlarmInstance> updateTask = new AsyncTask<Void, Void, AlarmInstance>() {

        @Override
        protected AlarmInstance doInBackground(Void... parameters) {
            if (context != null && alarm != null) {
                ContentResolver cr = context.getContentResolver();
                // Add alarm to db
                Alarm newAlarm = Alarm.addAlarm(cr, alarm);
                mScrollToAlarmId = newAlarm.id;
                if (expand) {
                    mExpandedId = newAlarm.id;
                }
                // Create and add instance to db
                if (newAlarm.enabled) {
                    sDeskClockExtensions.addAlarm(context, newAlarm);
                    return setupAlarmInstance(context, newAlarm);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(AlarmInstance instance) {
            if (instance != null) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                boolean noAlarmSoundHintShown = prefs.getBoolean(PREF_KEY_NO_ALARM_SOUND_HINT_SHOWN, false);
                boolean hintShown = prefs.getBoolean(PREF_KEY_ALARM_HINT_SHOWN, false);
                if (!instance.mRingtone.equals(Alarm.NO_RINGTONE_URI)) {
                    Uri defaultAlarm = Utils.getDefaultAlarmUri(getActivity());
                    if (defaultAlarm == null) {
                        if (!noAlarmSoundHintShown) {
                            // show hint that this alarm has been created with alarm tone choosen
                            AlarmUtils.popNoDefaultAlarmSoundToast(context);
                            prefs.edit().putBoolean(PREF_KEY_NO_ALARM_SOUND_HINT_SHOWN, true).commit();
                            return;
                        }
                    }
                }
                if (!hintShown) {
                    AlarmUtils.popFirstAlarmCreatedToast(context);
                    prefs.edit().putBoolean(PREF_KEY_ALARM_HINT_SHOWN, true).commit();
                } else {
                    AlarmUtils.popAlarmSetToast(context, instance.getAlarmTime().getTimeInMillis());
                }
            }
        }
    };
    updateTask.execute();
}
Also used : Context(android.content.Context) AlarmInstance(org.omnirom.deskclock.provider.AlarmInstance) SharedPreferences(android.content.SharedPreferences) Alarm(org.omnirom.deskclock.provider.Alarm) AsyncTask(android.os.AsyncTask) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 4 with AlarmInstance

use of org.omnirom.deskclock.provider.AlarmInstance in project android_packages_apps_OmniClock by omnirom.

the class AlarmClockFragment method asyncUpdateAlarm.

private void asyncUpdateAlarm(final Alarm alarm, final boolean popToast) {
    final Context context = getActivity().getApplicationContext();
    final AsyncTask<Void, Void, AlarmInstance> updateTask = new AsyncTask<Void, Void, AlarmInstance>() {

        @Override
        protected AlarmInstance doInBackground(Void... parameters) {
            ContentResolver cr = context.getContentResolver();
            // Dismiss all old instances
            AlarmStateManager.deleteAllInstances(context, alarm.id);
            // Update alarm
            Alarm.updateAlarm(cr, alarm);
            if (alarm.enabled) {
                return setupAlarmInstance(context, alarm);
            }
            return null;
        }

        @Override
        protected void onPostExecute(AlarmInstance instance) {
            if (popToast && instance != null) {
                AlarmUtils.popAlarmSetToast(context, instance.getAlarmTime().getTimeInMillis());
            }
        }
    };
    updateTask.execute();
}
Also used : Context(android.content.Context) AlarmInstance(org.omnirom.deskclock.provider.AlarmInstance) AsyncTask(android.os.AsyncTask) ContentResolver(android.content.ContentResolver)

Example 5 with AlarmInstance

use of org.omnirom.deskclock.provider.AlarmInstance in project android_packages_apps_OmniClock by omnirom.

the class AlarmStateManager method handleIntent.

private void handleIntent(Context context, Intent intent) {
    final String action = intent.getAction();
    LogUtils.v("AlarmStateManager received intent " + intent);
    if (CHANGE_STATE_ACTION.equals(action)) {
        Uri uri = intent.getData();
        AlarmInstance instance = AlarmInstance.getInstance(context.getContentResolver(), AlarmInstance.getId(uri));
        if (instance == null) {
            // Not a big deal, but it shouldn't happen
            LogUtils.e("Can not change state for unknown instance: " + uri);
            return;
        }
        int globalId = getGlobalIntentId(context);
        int intentId = intent.getIntExtra(ALARM_GLOBAL_ID_EXTRA, -1);
        int alarmState = intent.getIntExtra(ALARM_STATE_EXTRA, -1);
        if (intentId != globalId) {
            LogUtils.i("IntentId: " + intentId + " GlobalId: " + globalId + " AlarmState: " + alarmState);
            // Allows dismiss/snooze requests to go through
            if (!intent.hasCategory(ALARM_DISMISS_TAG) && !intent.hasCategory(ALARM_SNOOZE_TAG)) {
                LogUtils.i("Ignoring old Intent");
                return;
            }
        }
        LogUtils.v("AlarmStateManager change: " + instance + " to " + alarmState);
        if (alarmState >= 0) {
            setAlarmState(context, instance, alarmState);
        } else {
            registerInstance(context, instance, true);
        }
        LogUtils.v("AlarmStateManager change to: " + instance);
    } else if (SHOW_AND_DISMISS_ALARM_ACTION.equals(action)) {
        Uri uri = intent.getData();
        AlarmInstance instance = AlarmInstance.getInstance(context.getContentResolver(), AlarmInstance.getId(uri));
        long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId;
        Intent viewAlarmIntent = Alarm.createIntent(context, DeskClock.class, alarmId);
        viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
        viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId);
        viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(viewAlarmIntent);
        setDismissState(context, instance);
    }
}
Also used : AlarmInstance(org.omnirom.deskclock.provider.AlarmInstance) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Uri(android.net.Uri) DeskClock(org.omnirom.deskclock.DeskClock)

Aggregations

AlarmInstance (org.omnirom.deskclock.provider.AlarmInstance)9 ContentResolver (android.content.ContentResolver)8 Context (android.content.Context)2 Uri (android.net.Uri)2 AsyncTask (android.os.AsyncTask)2 Alarm (org.omnirom.deskclock.provider.Alarm)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Calendar (java.util.Calendar)1 DeskClock (org.omnirom.deskclock.DeskClock)1