Android Open Source - lunatick Widget






From Project

Back to project page lunatick.

License

The source code is released under:

Copyright (c) 2014, a. bellenir All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1....

If you think the Android project lunatick listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bellenir.lunatick;
/*from   w  ww .j a  va  2  s. c o  m*/
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.*;
import android.content.pm.*;
import android.app.PendingIntent;
import android.app.AlarmManager;
import android.os.SystemClock;
import android.widget.RemoteViews;
import android.graphics.Bitmap;

import java.util.Date;
import java.text.SimpleDateFormat;
import android.widget.*;
import java.util.*;

public class Widget extends AppWidgetProvider {
  
  private static final int SLEEPTIEM = 30; //Seconds
  public void onReceive(Context c, Intent intent) {
    super.onReceive(c, intent);
    int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
    update(c, ids);
  }
  
  private Intent getAlarmClockIntent(Context c){
    PackageManager packageManager = c.getPackageManager();
    if (packageManager != null) {

      Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(
        Intent.CATEGORY_LAUNCHER).setComponent(
        new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock"));

      ResolveInfo resolved = packageManager.resolveActivity(AlarmClockIntent, PackageManager.MATCH_DEFAULT_ONLY);
      if (resolved != null) {
        return AlarmClockIntent;
      } else {
        return null;
      }
    }
    return null;
  }

  @Override
  public void onUpdate(Context c, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(c, appWidgetManager, appWidgetIds);
    mkUpdateAlarm(c, appWidgetIds);
    update(c, appWidgetIds);
  }
  
  private void update(Context context, int[] ids){
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

    //Bitmap bmp = Luna.getPhaseBitmap();
    Bitmap bmp = LunaTick.getLunaTickBmp(false);
    views.setImageViewBitmap(R.id.Lunatick, bmp);

    // Open alarm clock on click
    Intent alarmClockIntent = getAlarmClockIntent(context);
    if (alarmClockIntent != null) {
      PendingIntent pendingIntent = PendingIntent.getActivity(
        context, 0, alarmClockIntent, 0);
      views.setOnClickPendingIntent(R.id.Lunatick, pendingIntent);
    }
    AppWidgetManager
      .getInstance(context)
      .updateAppWidget(ids, views);
  }

  @Override
  public void onDisabled(Context context) {
    super.onDisabled(context);
    rmUpdateAlarm(context);
  }
  
  private void rmUpdateAlarm(Context c){
    final Intent intent = new Intent(c, Widget.class);
    final PendingIntent pending = PendingIntent.getBroadcast(c, 0, intent, 0);
    final AlarmManager alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);
  }
  
  private void mkUpdateAlarm(Context c, int[] widgetIds){
    final Intent intent = new Intent(c, Widget.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
    final PendingIntent pending = PendingIntent.getBroadcast(c, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);
    long interval = 1000*SLEEPTIEM;
    alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, pending);
  }
  
}




Java Source Code List

com.bellenir.lunatick.Info.java
com.bellenir.lunatick.LunaTick.java
com.bellenir.lunatick.Luna.java
com.bellenir.lunatick.Tick.java
com.bellenir.lunatick.Widget.java