Search in sources :

Example 51 with AlarmManager

use of android.app.AlarmManager in project Android-Error-Reporter by tomquist.

the class ExceptionReportService method sendReport.

private void sendReport(Intent intent) throws UnsupportedEncodingException, NameNotFoundException {
    Log.v(TAG, "Got request to report error: " + intent.toString());
    Uri server = getTargetUrl();
    boolean isManualReport = intent.getBooleanExtra(EXTRA_MANUAL_REPORT, false);
    boolean isReportOnFroyo = isReportOnFroyo();
    boolean isFroyoOrAbove = isFroyoOrAbove();
    if (isFroyoOrAbove && !isManualReport && !isReportOnFroyo) {
        // We don't send automatic reports on froyo or above
        Log.d(TAG, "Don't send automatic report on froyo");
        return;
    }
    Set<String> fieldsToSend = getFieldsToSend();
    String stacktrace = intent.getStringExtra(EXTRA_STACK_TRACE);
    String exception = intent.getStringExtra(EXTRA_EXCEPTION_CLASS);
    String message = intent.getStringExtra(EXTRA_MESSAGE);
    long availableMemory = intent.getLongExtra(EXTRA_AVAILABLE_MEMORY, -1l);
    long totalMemory = intent.getLongExtra(EXTRA_TOTAL_MEMORY, -1l);
    String dateTime = intent.getStringExtra(EXTRA_EXCEPTION_TIME);
    String threadName = intent.getStringExtra(EXTRA_THREAD_NAME);
    String extraMessage = intent.getStringExtra(EXTRA_EXTRA_MESSAGE);
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    addNameValuePair(params, fieldsToSend, "exStackTrace", stacktrace);
    addNameValuePair(params, fieldsToSend, "exClass", exception);
    addNameValuePair(params, fieldsToSend, "exDateTime", dateTime);
    addNameValuePair(params, fieldsToSend, "exMessage", message);
    addNameValuePair(params, fieldsToSend, "exThreadName", threadName);
    if (extraMessage != null)
        addNameValuePair(params, fieldsToSend, "extraMessage", extraMessage);
    if (availableMemory >= 0)
        addNameValuePair(params, fieldsToSend, "devAvailableMemory", availableMemory + "");
    if (totalMemory >= 0)
        addNameValuePair(params, fieldsToSend, "devTotalMemory", totalMemory + "");
    PackageManager pm = getPackageManager();
    try {
        PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
        addNameValuePair(params, fieldsToSend, "appVersionCode", packageInfo.versionCode + "");
        addNameValuePair(params, fieldsToSend, "appVersionName", packageInfo.versionName);
        addNameValuePair(params, fieldsToSend, "appPackageName", packageInfo.packageName);
    } catch (NameNotFoundException e) {
    }
    addNameValuePair(params, fieldsToSend, "devModel", android.os.Build.MODEL);
    addNameValuePair(params, fieldsToSend, "devSdk", android.os.Build.VERSION.SDK);
    addNameValuePair(params, fieldsToSend, "devReleaseVersion", android.os.Build.VERSION.RELEASE);
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost(server.toString());
    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    Log.d(TAG, "Created post request");
    try {
        httpClient.execute(post);
        Log.v(TAG, "Reported error: " + intent.toString());
    } catch (ClientProtocolException e) {
        // Ignore this kind of error
        Log.e(TAG, "Error while sending an error report", e);
    } catch (SSLException e) {
        Log.e(TAG, "Error while sending an error report", e);
    } catch (IOException e) {
        if (e instanceof SocketException && e.getMessage().contains("Permission denied")) {
            Log.e(TAG, "You don't have internet permission", e);
        } else {
            int maximumRetryCount = getMaximumRetryCount();
            int maximumExponent = getMaximumBackoffExponent();
            // Retry at a later point in time
            AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
            int exponent = intent.getIntExtra(EXTRA_CURRENT_RETRY_COUNT, 0);
            intent.putExtra(EXTRA_CURRENT_RETRY_COUNT, exponent + 1);
            PendingIntent operation = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            if (exponent >= maximumRetryCount) {
                // Discard error
                Log.w(TAG, "Error report reached the maximum retry count and will be discarded.\nStacktrace:\n" + stacktrace);
                return;
            }
            if (exponent > maximumExponent) {
                exponent = maximumExponent;
            }
            // backoff in ms
            long backoff = (1 << exponent) * 1000;
            alarmMgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + backoff, operation);
        }
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) SocketException(java.net.SocketException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) Uri(android.net.Uri) SSLException(javax.net.ssl.SSLException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) PackageManager(android.content.pm.PackageManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent)

Example 52 with AlarmManager

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

the class TrimDataService method setNextTrim.

public void setNextTrim(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long now = new Date().getTime();
    long alarm = now + AlarmManager.INTERVAL_DAY;
    Log.v("alarm_date", "auto trim " + new Date(alarm).toString());
    PendingIntent pendingIntent = PendingIntent.getService(context, TRIM_ID, new Intent(context, TrimDataService.class), 0);
    am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Date(java.util.Date)

Example 53 with AlarmManager

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

the class BootReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    AppSettings settings = AppSettings.getInstance(context);
    if (settings.timelineRefresh != 0) {
        // user only wants manual
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + settings.timelineRefresh;
        Log.v("alarm_date", "timeline " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, HomeFragment.HOME_REFRESH_ID, new Intent(context, TimelineRefreshService.class), 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.timelineRefresh, pendingIntent);
    }
    if (settings.mentionsRefresh != 0) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + settings.mentionsRefresh;
        Log.v("alarm_date", "mentions " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, MentionsFragment.MENTIONS_REFRESH_ID, new Intent(context, MentionsRefreshService.class), 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.mentionsRefresh, pendingIntent);
    }
    if (settings.dmRefresh != 0) {
        // user only wants manual
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + settings.dmRefresh;
        Log.v("alarm_date", "dircet message " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, DMFragment.DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.dmRefresh, pendingIntent);
    }
    if (settings.activityRefresh != 0) {
        // user only wants manual
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + settings.activityRefresh;
        Log.v("alarm_date", "activity " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, ActivityFragment.ACTIVITY_REFRESH_ID, new Intent(context, ActivityRefreshService.class), 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, settings.activityRefresh, pendingIntent);
    }
    if (settings.autoTrim) {
        // user only wants manual
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = new Date().getTime();
        long alarm = now + 1000 * 60;
        Log.v("alarm_date", "auto trim " + new Date(alarm).toString());
        PendingIntent pendingIntent = PendingIntent.getService(context, TRIM_ID, new Intent(context, TrimDataService.class), 0);
        am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
    }
    if (settings.pushNotifications) {
        context.startService(new Intent(context, CatchupPull.class));
    }
    ArrayList<ScheduledTweet> tweets = QueuedDataSource.getInstance(context).getScheduledTweets();
    for (ScheduledTweet s : tweets) {
        Intent serviceIntent = new Intent(context.getApplicationContext(), SendScheduledTweet.class);
        Log.v("talon_scheduled_tweets", "in boot text: " + s.text);
        serviceIntent.putExtra(ViewScheduledTweets.EXTRA_TEXT, s.text);
        serviceIntent.putExtra("account", s.account);
        serviceIntent.putExtra("alarm_id", s.alarmId);
        PendingIntent pi = getDistinctPendingIntent(serviceIntent, s.alarmId);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, s.time, pi);
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) ScheduledTweet(com.klinker.android.twitter.data.ScheduledTweet) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Date(java.util.Date) AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent)

Example 54 with AlarmManager

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

the class ConnectivityChangeReceiver method onReceive.

@Override
public void onReceive(final Context context, final Intent intent) {
    Log.v("talon_pull", "connectivity change: just starting receiver");
    AppSettings settings = AppSettings.getInstance(context);
    // we don't want to do anything here if talon pull isn't on
    if (!settings.pushNotifications) {
        Log.v("talon_pull", "connectivity change: stopping the receiver very early");
        return;
    }
    if (Utils.hasInternetConnection(context)) {
        Log.v("talon_pull", "connectivity change: network is available and talon pull is on");
        // we want to turn off the live streaming/talon pull because it is just wasting battery not working/looking for connection
        context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        long now = Calendar.getInstance().getTimeInMillis();
        // schedule it to begin in 1 min
        long alarm = now + 60000;
        PendingIntent pendingIntent = PendingIntent.getService(context, 236, new Intent(context, CatchupPull.class), 0);
        // cancel the old one, then start the new one in 1 min
        am.cancel(pendingIntent);
        am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
    } else {
        Log.v("talon_pull", "connectivity change: network not available but talon pull is on");
        // we want to turn off the live streaming/talon pull because it is just wasting battery not working/looking for connection
        context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) CatchupPull(com.klinker.android.twitter.services.CatchupPull)

Example 55 with AlarmManager

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

the class QueueTweets method onReceive.

@Override
public void onReceive(final Context context, final Intent intent) {
    Log.v("talon_queued", "connectivity change: just starting receiver");
    AppSettings settings = AppSettings.getInstance(context);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long now = Calendar.getInstance().getTimeInMillis();
    // schedule it to begin in 20 seconds
    long alarm = now + 10000;
    PendingIntent pendingIntent = PendingIntent.getService(context, 253, new Intent(context, SendQueue.class), 0);
    // cancel the old one, then start the new one in 1 min
    am.cancel(pendingIntent);
    if (Utils.hasInternetConnection(context) && QueuedDataSource.getInstance(context).getQueuedTweets(settings.currentAccount).length > 0) {
        Log.v("talon_queued", "setting alarm for queue");
        am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) SendQueue(com.klinker.android.twitter.services.SendQueue)

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