Search in sources :

Example 56 with AlarmManager

use of android.app.AlarmManager in project Talon-for-Twitter by klinker24.

the class PrefFragment method onSharedPreferenceChanged.

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {
    SharedPreferences worldPrefs = getActivity().getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    // get the values and write them to our world prefs
    try {
        String s = sharedPrefs.getString(key, "");
        worldPrefs.edit().putString(key, s).commit();
    } catch (Exception e) {
        try {
            int i = sharedPrefs.getInt(key, -100);
            worldPrefs.edit().putInt(key, i).commit();
        } catch (Exception x) {
            try {
                boolean b = sharedPrefs.getBoolean(key, false);
                worldPrefs.edit().putBoolean(key, b).commit();
            } catch (Exception m) {
            }
        }
    }
    AppSettings.invalidate();
    if (key.equals("timeline_sync_interval")) {
        long refreshRate = Long.parseLong(sharedPrefs.getString("timeline_sync_interval", "1800000"));
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + refreshRate;
        Log.v("alarm_date", "timeline " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, HomeFragment.HOME_REFRESH_ID, new Intent(context, TimelineRefreshService.class), 0);
        if (refreshRate != 0)
            am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, refreshRate, pendingIntent);
        else
            am.cancel(pendingIntent);
    } else if (key.equals("mentions_sync_interval")) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long refreshRate = Long.parseLong(sharedPrefs.getString("mentions_sync_interval", "1800000"));
        long now = new Date().getTime();
        long alarm = now + refreshRate;
        Log.v("alarm_date", "mentions " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, MentionsFragment.MENTIONS_REFRESH_ID, new Intent(context, MentionsRefreshService.class), 0);
        if (refreshRate != 0)
            am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, refreshRate, pendingIntent);
        else
            am.cancel(pendingIntent);
    } else if (key.equals("dm_sync_interval")) {
        long refreshRate = Long.parseLong(sharedPrefs.getString("dm_sync_interval", "1800000"));
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + refreshRate;
        Log.v("alarm_date", "direct message " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, DMFragment.DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
        if (refreshRate != 0)
            am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, refreshRate, pendingIntent);
        else
            am.cancel(pendingIntent);
    } else if (key.equals("layout")) {
        new TrimCache(null).execute();
        context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
    } else if (key.equals("alert_types")) {
        Log.v("notification_set", "alert being set");
        Set<String> set = sharedPrefs.getStringSet("alert_types", null);
        if (set == null) {
            return;
        }
        if (set.contains("1")) {
            sharedPrefs.edit().putBoolean("vibrate", true).commit();
            worldPrefs.edit().putBoolean("vibrate", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("vibrate", false).commit();
            worldPrefs.edit().putBoolean("vibrate", false).commit();
        }
        if (set.contains("2")) {
            sharedPrefs.edit().putBoolean("led", true).commit();
            worldPrefs.edit().putBoolean("led", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("led", false).commit();
            worldPrefs.edit().putBoolean("led", false).commit();
        }
        if (set.contains("3")) {
            sharedPrefs.edit().putBoolean("wake", true).commit();
            worldPrefs.edit().putBoolean("wake", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("wake", false).commit();
            worldPrefs.edit().putBoolean("wake", false).commit();
        }
        if (set.contains("4")) {
            sharedPrefs.edit().putBoolean("sound", true).commit();
            worldPrefs.edit().putBoolean("sound", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("sound", false).commit();
            worldPrefs.edit().putBoolean("sound", false).commit();
        }
    } else if (key.equals("timeline_set")) {
        Log.v("notification_set", "timeline being set");
        Set<String> set = sharedPrefs.getStringSet("timeline_set", null);
        if (set == null) {
            return;
        }
        if (set.contains("1")) {
            sharedPrefs.edit().putBoolean("timeline_notifications", true).commit();
            worldPrefs.edit().putBoolean("timeline_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("timeline_notifications", false).commit();
            worldPrefs.edit().putBoolean("timeline_notifications", false).commit();
        }
        if (set.contains("2")) {
            sharedPrefs.edit().putBoolean("mentions_notifications", true).commit();
            worldPrefs.edit().putBoolean("mentions_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("mentions_notifications", false).commit();
            worldPrefs.edit().putBoolean("mentions_notifications", false).commit();
        }
        if (set.contains("3")) {
            sharedPrefs.edit().putBoolean("direct_message_notifications", true).commit();
            worldPrefs.edit().putBoolean("direct_message_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("direct_message_notifications", false).commit();
            worldPrefs.edit().putBoolean("direct_message_notifications", false).commit();
        }
        if (set.contains("4")) {
            sharedPrefs.edit().putBoolean("activity_notifications", true).commit();
            worldPrefs.edit().putBoolean("activity_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("activity_notifications", false).commit();
            worldPrefs.edit().putBoolean("activity_notifications", false).commit();
        }
    } else if (key.equals("interactions_set")) {
        Log.v("notification_set", "interactions being set");
        Set<String> set = sharedPrefs.getStringSet("interactions_set", null);
        if (set == null) {
            return;
        }
        if (set.contains("1")) {
            sharedPrefs.edit().putBoolean("favorite_notifications", true).commit();
            worldPrefs.edit().putBoolean("favorite_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("favorite_notifications", false).commit();
            worldPrefs.edit().putBoolean("favorite_notifications", false).commit();
        }
        if (set.contains("2")) {
            sharedPrefs.edit().putBoolean("retweet_notifications", true).commit();
            worldPrefs.edit().putBoolean("retweet_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("retweet_notifications", false).commit();
            worldPrefs.edit().putBoolean("retweet_notifications", false).commit();
        }
        if (set.contains("3")) {
            sharedPrefs.edit().putBoolean("follower_notifications", true).commit();
            worldPrefs.edit().putBoolean("follower_notifications", true).commit();
        } else {
            sharedPrefs.edit().putBoolean("follower_notifications", false).commit();
            worldPrefs.edit().putBoolean("follower_notifications", false).commit();
        }
    } else if (key.equals("widget_theme") || key.equals("text_size")) {
        context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) TimelineRefreshService(com.klinker.android.twitter.services.TimelineRefreshService) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) TwitterException(twitter4j.TwitterException) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) DirectMessageRefreshService(com.klinker.android.twitter.services.DirectMessageRefreshService)

Example 57 with AlarmManager

use of android.app.AlarmManager in project Talon-for-Twitter by klinker24.

the class PrefFragment method setUpSyncSettings.

public void setUpSyncSettings() {
    final Context context = getActivity();
    final AppSettings settings = AppSettings.getInstance(context);
    final SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    final Preference timeline = findPreference("timeline_sync_interval");
    final Preference mentions = findPreference("mentions_sync_interval");
    final Preference dms = findPreference("dm_sync_interval");
    final Preference onStart = findPreference("refresh_on_start");
    final Preference mobileOnly = findPreference("sync_mobile_data");
    int count = 0;
    if (sharedPrefs.getBoolean("is_logged_in_1", false)) {
        count++;
    }
    if (sharedPrefs.getBoolean("is_logged_in_2", false)) {
        count++;
    }
    final boolean mentionsChanges = count == 2;
    if (sharedPrefs.getBoolean("push_notifications", true)) {
        if (sharedPrefs.getBoolean("live_streaming", true)) {
            timeline.setEnabled(false);
            onStart.setEnabled(false);
        }
        if (!mentionsChanges) {
            mentions.setEnabled(false);
            dms.setEnabled(false);
            mobileOnly.setEnabled(false);
        }
    }
    final Preference fillGaps = findPreference("fill_gaps");
    fillGaps.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            new FillGaps().execute();
            return false;
        }
    });
    final Preference stream = findPreference("live_streaming");
    stream.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
            if (((CheckBoxPreference) stream).isChecked()) {
                timeline.setEnabled(true);
                onStart.setEnabled(true);
            } else {
                timeline.setEnabled(false);
                onStart.setEnabled(false);
            }
            return true;
        }
    });
    final Preference noti = findPreference("show_pull_notification");
    noti.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
            return true;
        }
    });
    final Preference pull = findPreference("push_notifications");
    pull.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if (!((CheckBoxPreference) pull).isChecked()) {
                if (sharedPrefs.getBoolean("live_streaming", true)) {
                    timeline.setEnabled(false);
                    onStart.setEnabled(false);
                }
                mobileOnly.setEnabled(false);
                if (!mentionsChanges) {
                    mentions.setEnabled(false);
                    dms.setEnabled(false);
                }
                AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                PendingIntent pendingIntent1 = PendingIntent.getService(context, HomeFragment.HOME_REFRESH_ID, new Intent(context, TimelineRefreshService.class), 0);
                PendingIntent pendingIntent2 = PendingIntent.getService(context, MentionsFragment.MENTIONS_REFRESH_ID, new Intent(context, MentionsRefreshService.class), 0);
                PendingIntent pendingIntent3 = PendingIntent.getService(context, DMFragment.DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
                am.cancel(pendingIntent1);
                am.cancel(pendingIntent2);
                am.cancel(pendingIntent3);
                SharedPreferences.Editor e = sharedPrefs.edit();
                if (sharedPrefs.getBoolean("live_streaming", true)) {
                    e.putString("timeline_sync_interval", "0");
                }
                e.putString("mentions_sync_interval", "0");
                e.putString("dm_sync_interval", "0");
                e.commit();
            } else {
                timeline.setEnabled(true);
                mentions.setEnabled(true);
                dms.setEnabled(true);
                onStart.setEnabled(true);
                mobileOnly.setEnabled(true);
            }
            return true;
        }
    });
    Preference sync = findPreference("sync_friends");
    sync.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference arg0) {
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.sync_friends)).setMessage(context.getResources().getString(R.string.sync_friends_summary)).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    try {
                        new SyncFriends(settings.myScreenName, sharedPrefs).execute();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).create().show();
            return false;
        }
    });
    if (count != 2) {
        ((PreferenceGroup) findPreference("other_options")).removePreference(findPreference("sync_second_mentions"));
    }
// remove the mobile data one if they have a tablet
/*if (context.getResources().getBoolean(R.bool.isTablet)) {
            getPreferenceScreen().removePreference(getPreferenceManager().findPreference("sync_mobile_data"));
        }*/
}
Also used : Context(android.content.Context) AlertDialog(android.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) DialogInterface(android.content.DialogInterface) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) TwitterException(twitter4j.TwitterException) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent)

Example 58 with AlarmManager

use of android.app.AlarmManager in project Rutgers-Course-Tracker by tevjef.

the class Alarm method setAlarm.

public void setAlarm() {
    AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent service = new Intent(mContext, RequestService.class);
    //Package that intent into a pending intent.
    PendingIntent alarmIntent = PendingIntent.getService(mContext, 1234, service, PendingIntent.FLAG_UPDATE_CURRENT);
    //Cancel all previous alarms of the same pending intent.
    alarmMgr.cancel(alarmIntent);
    //Set the alarm to the alarm manager.
    alarmMgr.setInexactRepeating(//Time since the device booted.
    AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), getInterval(), alarmIntent);
    Timber.d("Alarm set: " + getInterval());
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 59 with AlarmManager

use of android.app.AlarmManager in project platform_frameworks_base by android.

the class BluetoothDiscoverableTimeoutReceiver method setDiscoverableAlarm.

public static void setDiscoverableAlarm(Context context, long alarmTime) {
    Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime);
    Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
    intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (pending != null) {
        // Cancel any previous alarms that do the same thing.
        alarmManager.cancel(pending);
        Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm");
    }
    pending = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 60 with AlarmManager

use of android.app.AlarmManager in project platform_frameworks_base by android.

the class NetworkStatsService method create.

public static NetworkStatsService create(Context context, INetworkManagementService networkManager) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    NetworkStatsService service = new NetworkStatsService(context, networkManager, alarmManager, wakeLock, NtpTrustedTime.getInstance(context), TelephonyManager.getDefault(), new DefaultNetworkStatsSettings(context), new NetworkStatsObservers(), getDefaultSystemDir(), getDefaultBaseDir());
    HandlerThread handlerThread = new HandlerThread(TAG);
    Handler.Callback callback = new HandlerCallback(service);
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper(), callback);
    service.setHandler(handler, callback);
    return service;
}
Also used : PowerManager(android.os.PowerManager) HandlerThread(android.os.HandlerThread) INetworkStatsService(android.net.INetworkStatsService) IAlarmManager(android.app.IAlarmManager) AlarmManager(android.app.AlarmManager) Handler(android.os.Handler)

Aggregations

AlarmManager (android.app.AlarmManager)471 PendingIntent (android.app.PendingIntent)349 Intent (android.content.Intent)323 Calendar (java.util.Calendar)75 SuppressLint (android.annotation.SuppressLint)24 Date (java.util.Date)24 Context (android.content.Context)22 SharedPreferences (android.content.SharedPreferences)20 SimpleDateFormat (java.text.SimpleDateFormat)18 Test (org.junit.Test)17 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)13 Config (org.robolectric.annotation.Config)13 Handler (android.os.Handler)11 HashMap (java.util.HashMap)11 PowerManager (android.os.PowerManager)10 File (java.io.File)10 Map (java.util.Map)10 Activity (android.app.Activity)9 ShadowAlarmManager (org.robolectric.shadows.ShadowAlarmManager)9 TargetApi (android.annotation.TargetApi)8