/**
  * draw bottom/right half shadow
  *
  * @param canvas
  */
 private void drawNextShadow(Canvas canvas) {
   final float degreesFlipped = getDegreesFlipped();
   if (degreesFlipped < 90) {
     final int alpha = (int) ((Math.abs(degreesFlipped - 90) / 90f) * MAX_SHADOW_ALPHA);
     mShadowPaint.setAlpha(alpha);
     canvas.drawPaint(mShadowPaint);
   }
 }
示例#2
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    // GET SETTINGS FROM SHARED PREFERENCES
    SharedPreferences settingsF = getSharedPreferences(FUZZY_STUFF, 0);
    int backgroundColorF =
        settingsF.getInt("BackgroundColorF", this.getResources().getColor(R.color.palesky));
    int textColorF = settingsF.getInt("TextColorF", this.getResources().getColor(R.color.night));
    String packageName = settingsF.getString("PackageName", "com.android.alarmclock");
    String className = settingsF.getString("ClassName", "com.android.alarmclock.AlarmClock");

    // CALCULATE ALL THE TIME DEPENDENT VARIABLES

    // reset the time dependent addition
    long alarmAddMillis = 0;

    // get time instance and values
    Calendar rightNow = Calendar.getInstance();
    int thisMin = rightNow.get(Calendar.MINUTE);
    int thisHour = rightNow.get(Calendar.HOUR);

    // set correct hour values
    nextHour = thisHour + 1;
    if (thisHour == 0) {
      thisHour = 12;
    }
    if (nextHour == 13) {
      nextHour = 1;
    }

    // set text values based on minutes
    if (thisMin >= 0 && thisMin <= 7) {
      wordA = "its";
      wordB = "about";
      wordA_B = "its_about";
      clockH = thisHour;
      nextAlarmMin = 8;
      alarmAddMillis = 0;
    }
    if (thisMin >= 8 && thisMin <= 22) {
      wordA = "quarter";
      wordB = "past";
      wordA_B = "quarter_past";
      clockH = thisHour;
      nextAlarmMin = 23;
      alarmAddMillis = 0;
    }
    if (thisMin >= 23 && thisMin <= 37) {
      wordA = "half";
      wordB = "past";
      wordA_B = "half_past";
      clockH = thisHour;
      nextAlarmMin = 38;
      alarmAddMillis = 0;
    }
    if (thisMin >= 38 && thisMin <= 52) {
      wordA = "quarter";
      wordB = "to";
      wordA_B = "quarter_to";
      clockH = nextHour;
      nextAlarmMin = 53;
      alarmAddMillis = 0;
    }
    if (thisMin >= 53 && thisMin <= 59) {
      wordA = "its";
      wordB = "about";
      wordA_B = "its_about";
      clockH = nextHour;
      nextAlarmMin = 8;
      alarmAddMillis = 3600000;
    }

    // CANCEL ANY UNSENT ALARM
    if (alarmManager != null) {
      alarmManager.cancel(pendingIntent);
    }

    // SET UP THE NEXT ALARM

    // set the time
    Calendar nextAlarm = Calendar.getInstance();
    // add one hour of millis if its for the next hour
    nextAlarm.setTimeInMillis(System.currentTimeMillis() + alarmAddMillis);
    // set the correct minute
    nextAlarm.set(Calendar.MINUTE, nextAlarmMin);
    nextAlarm.set(Calendar.SECOND, 15);
    nextAlarm.set(Calendar.MILLISECOND, 0);

    // request the alarm
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent usIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, usIntent, 0);
    // use onExact for API19+
    int currentApiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentApiVersion <= 18) {
      alarmManager.set(AlarmManager.RTC, nextAlarm.getTimeInMillis(), pendingIntent);
    } else {
      alarmManager.setExact(AlarmManager.RTC, nextAlarm.getTimeInMillis(), pendingIntent);
    }

    // SET UP THE IMAGES

    // get the resource id ints for images to send by remoteviews
    int timePhraseA =
        this.getResources().getIdentifier("phr_" + wordA, "drawable", this.getPackageName());
    int timePhraseB =
        this.getResources().getIdentifier("phr_" + wordB, "drawable", this.getPackageName());
    int timePhraseA_B =
        this.getResources().getIdentifier("phr_" + wordA_B, "drawable", this.getPackageName());
    int hourWordImageCrop =
        this.getResources().getIdentifier("num_" + clockH, "drawable", this.getPackageName());
    int hourWordImageWide =
        this.getResources()
            .getIdentifier("num_" + clockH + "_w", "drawable", this.getPackageName());

    // SET THE BITMAP COLOR AND OTHER VALUES
    fillPaintF = new Paint(Paint.FILTER_BITMAP_FLAG);
    fillPaintF.setColor(textColorF);
    fillPaintF.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    // ASSEMBLE THE PENDING INTENTS

    // create the open configuration pending intent
    Intent openAWC = new Intent(this, AppWidgetConfigure.class);
    openAWC.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent openAwcPi =
        PendingIntent.getActivity(this, 0, openAWC, PendingIntent.FLAG_UPDATE_CURRENT);

    // create the open alarms pending intent - done here because it couldnt be saved in prefs
    // set up open Alarms Intent
    PendingIntent alarmPI = null;
    PackageManager packageManager = this.getPackageManager();
    Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
    // get the activity info and attach to the intent
    try {
      ComponentName cn = new ComponentName(packageName, className);
      packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
      alarmClockIntent.setComponent(cn);
    } catch (PackageManager.NameNotFoundException e) {
      stopSelf();
    }
    // finalize the pending intent
    alarmPI = PendingIntent.getActivity(this, 0, alarmClockIntent, 0);

    // create the refresh widget pending intent
    Intent startUpdateF = new Intent(this, UpdateService.class);
    startUpdateF.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    pendingStartUpdateF =
        PendingIntent.getService(this, 0, startUpdateF, PendingIntent.FLAG_UPDATE_CURRENT);

    // **** BACKGROUND IMAGE PREPARATION ****

    // draw an empty image
    bitmapBackgroundF = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    // paint in the color
    Canvas canvasBackgroundF = new Canvas(bitmapBackgroundF);
    fillPaintBackgroundF = new Paint();
    fillPaintBackgroundF.setColor(backgroundColorF);
    canvasBackgroundF.drawPaint(fillPaintBackgroundF);

    // SEND ALL THE STUFF TO THE PLACED WIDGETS

    // get appwidgetmanager instance for all widgets
    AppWidgetManager localAppWidgetManager = AppWidgetManager.getInstance(this);

    // update all 4x1 widget instances
    ComponentName thisWidget4x1 = new ComponentName(getBaseContext(), WidgetProvider4x1.class);
    int[] allWidgetIds4x1 = localAppWidgetManager.getAppWidgetIds(thisWidget4x1);
    if (allWidgetIds4x1 != null) {

      // join the images together into one strip
      Bitmap bitmap4x1partA = BitmapFactory.decodeResource(getResources(), timePhraseA, options);
      Bitmap bitmap4x1partB = BitmapFactory.decodeResource(getResources(), timePhraseB, options);
      Bitmap bitmap4x1partC =
          BitmapFactory.decodeResource(getResources(), hourWordImageCrop, options);

      Bitmap bitmap4x1strip =
          Bitmap.createBitmap(
              bitmap4x1partA.getWidth() + bitmap4x1partB.getWidth() + bitmap4x1partC.getWidth(),
              bitmap4x1partA.getHeight(),
              Bitmap.Config.ARGB_8888);
      Canvas canvas4x1strip = new Canvas(bitmap4x1strip);
      canvas4x1strip.drawBitmap(bitmap4x1partA, 0, 0, null);
      canvas4x1strip.drawBitmap(bitmap4x1partB, bitmap4x1partA.getWidth(), 0, null);
      canvas4x1strip.drawBitmap(
          bitmap4x1partC, bitmap4x1partA.getWidth() + bitmap4x1partB.getWidth(), 0, null);

      // generate adjusted images if color theme isnt default

      // strip
      Bitmap bitmap4x1strip2 = bitmap4x1strip.copy(Bitmap.Config.ARGB_8888, true);
      Canvas canvas4x1strip2 = new Canvas(bitmap4x1strip2);
      canvas4x1strip2.drawPaint(fillPaintF);

      for (int widgetId : allWidgetIds4x1) {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout_4x1);
        remoteViews.setImageViewBitmap(R.id.imageviewBG, bitmapBackgroundF);
        remoteViews.setImageViewBitmap(R.id.imageviewABC, bitmap4x1strip2);
        remoteViews.setOnClickPendingIntent(R.id.topclickLeft, alarmPI);
        remoteViews.setOnClickPendingIntent(R.id.topclickRight, openAwcPi);
        localAppWidgetManager.updateAppWidget(widgetId, remoteViews);
      }
      bitmap4x1strip2 = null;
      canvas4x1strip2 = null;
      bitmap4x1strip = null;
      canvas4x1strip = null;
    }

    // update all 3x2 widget instances
    ComponentName thisWidget3x2 = new ComponentName(getBaseContext(), WidgetProvider3x2.class);
    int[] allWidgetIds3x2 = localAppWidgetManager.getAppWidgetIds(thisWidget3x2);
    if (allWidgetIds3x2 != null) {
      // generate adjusted images if color theme isnt default
      // time phrase
      if (bitmapTimePhraseA_B == null) {
        bitmapTimePhraseA_B1 = BitmapFactory.decodeResource(getResources(), timePhraseA_B, options);
        bitmapTimePhraseA_B = bitmapTimePhraseA_B1.copy(Bitmap.Config.ARGB_8888, true);
        canvasPhrase = new Canvas(bitmapTimePhraseA_B);
        canvasPhrase.drawPaint(fillPaintF);
      }
      // hour word
      if (bitmapHourWordImage == null) {
        bitmapHourWordImage1 =
            BitmapFactory.decodeResource(getResources(), hourWordImageWide, options);
        bitmapHourWordImage = bitmapHourWordImage1.copy(Bitmap.Config.ARGB_8888, true);
        canvasHour = new Canvas(bitmapHourWordImage);
        canvasHour.drawPaint(fillPaintF);
      }
      for (int widgetId : allWidgetIds3x2) {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout_3x2);
        remoteViews.setImageViewBitmap(R.id.imageviewA_B, bitmapTimePhraseA_B);
        remoteViews.setImageViewBitmap(R.id.imageviewC, bitmapHourWordImage);
        remoteViews.setImageViewBitmap(R.id.imageviewBG, bitmapBackgroundF);
        remoteViews.setOnClickPendingIntent(R.id.topclickLeft, alarmPI);
        remoteViews.setOnClickPendingIntent(R.id.topclickRight, openAwcPi);
        localAppWidgetManager.updateAppWidget(widgetId, remoteViews);
      }
    }

    // CLEAR ALL THE BITMAPS
    bitmapTimePhraseA_B = null;
    bitmapTimePhraseA_B1 = null;
    bitmapHourWordImageCrop = null;
    bitmapHourWordImageCrop1 = null;
    bitmapHourWordImage = null;
    bitmapHourWordImage1 = null;
    canvasPhrase = null;
    canvasHour = null;
    bitmapBackgroundF = null;

    stopSelf();
    return super.onStartCommand(intent, flags, startId);
  }