From a5be3c0693cbb73056c89d077e9c1fb8da4e595f Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Fri, 12 Feb 2016 22:09:33 -0500 Subject: [PATCH] Simplify interface, fix wakelock bug --- SimpleTimer.iml | 12 +- gradle/wrapper/gradle-wrapper.properties | 4 +- mobile/mobile.iml | 136 ------- wear/src/main/AndroidManifest.xml | 2 +- .../java/org/isoron/base/ColorHelper.java | 25 ++ .../org/isoron/simpletimer/MainActivity.java | 117 +++--- .../isoron/simpletimer/model/SimpleTimer.java | 59 +++ .../simpletimer/{ => views}/TimerView.java | 335 +++++++----------- .../main/res/layout/round_activity_main.xml | 2 +- wear/src/main/res/values/styles.xml | 6 + wear/wear.iml | 102 ------ 11 files changed, 305 insertions(+), 495 deletions(-) delete mode 100644 mobile/mobile.iml create mode 100644 wear/src/main/java/org/isoron/base/ColorHelper.java create mode 100644 wear/src/main/java/org/isoron/simpletimer/model/SimpleTimer.java rename wear/src/main/java/org/isoron/simpletimer/{ => views}/TimerView.java (52%) create mode 100644 wear/src/main/res/values/styles.xml delete mode 100644 wear/wear.iml diff --git a/SimpleTimer.iml b/SimpleTimer.iml index d557092..3d1ae5a 100644 --- a/SimpleTimer.iml +++ b/SimpleTimer.iml @@ -1,19 +1,19 @@ - + - - + - - + + - + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c71e76..9784ff5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Fri Feb 12 11:34:36 EST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip diff --git a/mobile/mobile.iml b/mobile/mobile.iml deleted file mode 100644 index df29754..0000000 --- a/mobile/mobile.iml +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml index 136ce76..3b82a28 100644 --- a/wear/src/main/AndroidManifest.xml +++ b/wear/src/main/AndroidManifest.xml @@ -8,7 +8,7 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name_simple" - android:theme="@android:style/Theme.DeviceDefault"> + android:theme="@style/AppTheme"> > ALPHA_CHANNEL & 0xff) * amount) + + ((float) (color2 >> ALPHA_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int r = ((int) (((float) (color1 >> RED_CHANNEL & 0xff) * amount) + + ((float) (color2 >> RED_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int g = ((int) (((float) (color1 >> GREEN_CHANNEL & 0xff) * amount) + + ((float) (color2 >> GREEN_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int b = ((int) (((float) (color1 & 0xff) * amount) + + ((float) (color2 & 0xff) * inverseAmount))) & 0xff; + + return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL; + } +} diff --git a/wear/src/main/java/org/isoron/simpletimer/MainActivity.java b/wear/src/main/java/org/isoron/simpletimer/MainActivity.java index 6f40689..e67878b 100644 --- a/wear/src/main/java/org/isoron/simpletimer/MainActivity.java +++ b/wear/src/main/java/org/isoron/simpletimer/MainActivity.java @@ -4,12 +4,15 @@ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.support.wearable.activity.WearableActivity; import android.support.wearable.view.WatchViewStub; import android.util.Log; import org.isoron.base.AmbientModeListener; +import org.isoron.simpletimer.model.SimpleTimer; +import org.isoron.simpletimer.views.TimerView; import java.util.Timer; import java.util.TimerTask; @@ -18,13 +21,16 @@ public class MainActivity extends WearableActivity { private static final String TAG = "MainActivity"; + public static final String PREFS_NAME = "SimpleTimerPrefs"; + public static final int DEFAULT_INITIAL_TIME = 5 * 60 * 1000; + AmbientModeListener ambientModeListener = null; TimerView timerView; - private AlarmManager ambientModeAlarmManager; + private AlarmManager alarmManager; private PendingIntent ambientModePendingIntent; - private Timer timer; - private boolean isActive = false; + private Timer fixedRateTimer; + private SimpleTimer stimer; @Override protected void onCreate(Bundle savedInstanceState) @@ -34,15 +40,18 @@ public class MainActivity extends WearableActivity setAmbientEnabled(); - ambientModeAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + stimer = new SimpleTimer(); + alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent ambientModeIntent = new Intent(getApplicationContext(), MainActivity.class); ambientModeIntent.setAction("REFRESH"); - - ambientModePendingIntent = - PendingIntent.getActivity(getApplicationContext(), 0, ambientModeIntent, + ambientModePendingIntent = PendingIntent + .getActivity(getApplicationContext(), 0, ambientModeIntent, PendingIntent.FLAG_UPDATE_CURRENT); + SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0); + final long initialTime = preferences.getLong("initialTime", DEFAULT_INITIAL_TIME); + final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @@ -50,19 +59,21 @@ public class MainActivity extends WearableActivity public void onLayoutInflated(WatchViewStub stub) { timerView = (TimerView) findViewById(R.id.timerview); + timerView.setTime(initialTime); + timerView.setTimer(stimer); setAmbientModeListener(timerView); - - startTimer(); - refresh(); + startFixedRateTimer(); + refreshViews(); } }); } - private void startTimer() + private void startFixedRateTimer() { - if (timer != null) timer.cancel(); - timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() + if (fixedRateTimer != null) fixedRateTimer.cancel(); + + fixedRateTimer = new Timer(); + fixedRateTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() @@ -72,44 +83,38 @@ public class MainActivity extends WearableActivity @Override public void run() { - refresh(); + refreshViews(); } }); } }, 0, 1000); } - private void stopTimer() + private void stopFixedRateTimer() { - if (timer != null) timer.cancel(); + if (fixedRateTimer != null) fixedRateTimer.cancel(); } - private void refresh() + private void refreshViews() { - Log.d(TAG, "refresh() ambient? " + isAmbient() + " active? " + isActive); - if (timerView != null) { timerView.tick(); timerView.invalidate(); } - if (isAmbient() || !isActive) - { - long delay = -1; - if (timerView != null) - { - if (isAmbient()) delay = timerView.getMillisecondsUntilNextMinute(); - else delay = timerView.getRemainingTime(); - } + if (isAmbient()) scheduleNextRefresh(); + } - if (delay > 0) - { - Log.d(TAG, "sleeping for " + delay + " milliseconds (" + delay / 1000 / 60.0 + " minutes)"); - ambientModeAlarmManager.setExact(AlarmManager.RTC_WAKEUP, - System.currentTimeMillis() + delay + 100, ambientModePendingIntent); - } - } + private void scheduleNextRefresh() + { + long delay = timerView.getMillisecondsUntilNextMinute(); + if (delay < 0) return; + + Log.d(TAG, "sleeping for " + delay + " milliseconds (" + delay / 1000 / 60.0 + + " minutes)"); + alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay + 100, + ambientModePendingIntent); } @Override @@ -117,8 +122,10 @@ public class MainActivity extends WearableActivity { super.onEnterAmbient(ambientDetails); if (ambientModeListener != null) ambientModeListener.onEnterAmbient(ambientDetails); - stopTimer(); - refresh(); + + stopFixedRateTimer(); + + refreshViews(); } @Override @@ -126,8 +133,10 @@ public class MainActivity extends WearableActivity { super.onExitAmbient(); if (ambientModeListener != null) ambientModeListener.onExitAmbient(); - startTimer(); - refresh(); + + startFixedRateTimer(); + + refreshViews(); } @Override @@ -135,6 +144,7 @@ public class MainActivity extends WearableActivity { Log.d(TAG, "onUpdateAmbient()"); super.onUpdateAmbient(); + if (ambientModeListener != null) ambientModeListener.onUpdateAmbient(); } @@ -151,7 +161,7 @@ public class MainActivity extends WearableActivity Log.d(TAG, "onNewIntent: " + intent.getAction()); - refresh(); + refreshViews(); } @Override @@ -159,27 +169,40 @@ public class MainActivity extends WearableActivity { super.onPause(); Log.d(TAG, "onPause()"); - isActive = false; - stopTimer(); - refresh(); + + stopFixedRateTimer(); + + refreshViews(); } @Override protected void onStop() { super.onStop(); + stopFixedRateTimer(); + + savePreferences(); + + if (!isAmbient()) finishAffinity(); + Log.d(TAG, "onStop()"); } + private void savePreferences() + { + long totalTime = timerView.getTime(); + SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putLong("initialTime", totalTime); + editor.commit(); + } + @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume()"); - - isActive = true; - if(!isAmbient()) startTimer(); - refresh(); + if (!isAmbient()) startFixedRateTimer(); } @Override diff --git a/wear/src/main/java/org/isoron/simpletimer/model/SimpleTimer.java b/wear/src/main/java/org/isoron/simpletimer/model/SimpleTimer.java new file mode 100644 index 0000000..e7af0e5 --- /dev/null +++ b/wear/src/main/java/org/isoron/simpletimer/model/SimpleTimer.java @@ -0,0 +1,59 @@ +package org.isoron.simpletimer.model; + +public class SimpleTimer +{ + private enum TimerState { + PAUSED, RUNNING + } + + private long startTime; + private long totalTime; + private long remainingTime; + private TimerState state; + + public static final int DEFAULT_INITIAL_TIME = 5 * 60 * 1000; + + public SimpleTimer() + { + startTime = -1; + remainingTime = totalTime = DEFAULT_INITIAL_TIME; + } + + public void resume() + { + if(isRunning()) return; + + startTime = System.currentTimeMillis(); + state = TimerState.RUNNING; + } + + public void pause() + { + if(isPaused()) return; + state = TimerState.PAUSED; + } + + public boolean isRunning() + { + return state == TimerState.RUNNING; + } + + public boolean isPaused() + { + return state == TimerState.PAUSED; + } + + public void reset() + { + state = TimerState.PAUSED; + remainingTime = totalTime; + } + + public long getRemainingTime() + { + long currentTime = System.currentTimeMillis(); + long elapsedTime = currentTime - startTime; + + return totalTime - elapsedTime; + } +} diff --git a/wear/src/main/java/org/isoron/simpletimer/TimerView.java b/wear/src/main/java/org/isoron/simpletimer/views/TimerView.java similarity index 52% rename from wear/src/main/java/org/isoron/simpletimer/TimerView.java rename to wear/src/main/java/org/isoron/simpletimer/views/TimerView.java index 96aaf0e..af51b65 100644 --- a/wear/src/main/java/org/isoron/simpletimer/TimerView.java +++ b/wear/src/main/java/org/isoron/simpletimer/views/TimerView.java @@ -1,4 +1,4 @@ -package org.isoron.simpletimer; +package org.isoron.simpletimer.views; import android.app.Activity; import android.content.Context; @@ -10,25 +10,29 @@ import android.graphics.RectF; import android.os.Bundle; import android.os.PowerManager; import android.os.Vibrator; +import android.support.wearable.activity.WearableActivity; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; -import android.view.WindowManager; import org.isoron.base.AmbientModeListener; +import org.isoron.base.ColorHelper; +import org.isoron.simpletimer.MainActivity; +import org.isoron.simpletimer.model.SimpleTimer; + +import java.util.Calendar; -import java.util.Timer; public class TimerView extends View implements AmbientModeListener { private static final String TAG = "TimerView"; - private final int primaryColor; - private final int secondaryColor; - private final int tertiaryColor; + private int primaryColor; + private int secondaryColor; + private int tertiaryColor; - private final int backgroundColor; + private int backgroundColor; private Paint paint; private Paint paintInteractive; @@ -39,8 +43,7 @@ public class TimerView extends View implements AmbientModeListener private int height; private int fontHeight; private int size; - private Timer timer; - private final Activity activity; + private final WearableActivity activity; private Vibrator vibrator; private int step; @@ -49,33 +52,56 @@ public class TimerView extends View implements AmbientModeListener private long remainingTime; private long lastTick; - private int brightnessCountdown; - private boolean isBright = false; - private boolean hasLongPressed; private boolean hasMoved = false; private boolean ambientMode = false; - private final int DEFAULT_TIME = 5 * 60 * 1000; private final int GRANULARITY = 60 * 1000; - private final int BRIGHTNESS_LENGTH = 5; private final long VIBRATION_FINISH[] = {0, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250}; private RectF screenRect; + private SimpleTimer stimer; + public TimerView(Context ctx, AttributeSet attrs) { super(ctx, attrs); - this.activity = (Activity) ctx; + this.activity = (WearableActivity) ctx; step = 0; - isRunning = false; - totalTime = DEFAULT_TIME; + totalTime = MainActivity.DEFAULT_INITIAL_TIME; remainingTime = totalTime; + isRunning = false; hasMoved = false; hasLongPressed = false; + stimer = null; + + initializePaints(); + initializeColors(); + + vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE); + + setLongClickable(true); + setOnTouchListener(new TouchListener()); + setOnLongClickListener(new LongClickListener()); + } + + public void setTimer(SimpleTimer stimer) + { + this.stimer = stimer; + } + + private void initializeColors() + { + primaryColor = Color.parseColor("#0288d1"); + secondaryColor = Color.WHITE; + tertiaryColor = ColorHelper.mixColors(primaryColor, Color.BLACK, 0.37f); + backgroundColor = Color.BLACK; + } + private void initializePaints() + { paintInteractive = new Paint(); paintInteractive.setColor(Color.parseColor("#B2FF59")); paintInteractive.setStyle(Paint.Style.FILL); @@ -91,96 +117,16 @@ public class TimerView extends View implements AmbientModeListener paintAmbient = new Paint(); paintAmbient.setColor(Color.WHITE); paintAmbient.setStyle(Paint.Style.FILL_AND_STROKE); - paintAmbient.setAntiAlias(false); + paintAmbient.setAntiAlias(true); paintAmbient.setTextAlign(Paint.Align.CENTER); - - primaryColor = Color.parseColor("#0288d1"); - secondaryColor = Color.WHITE; - tertiaryColor = mixColors(primaryColor, Color.BLACK, 0.37f); - backgroundColor = Color.BLACK; - - vibrator = (Vibrator) activity.getSystemService(Activity.VIBRATOR_SERVICE); - - setLongClickable(true); - setOnLongClickListener(new View.OnLongClickListener() - { - @Override - public boolean onLongClick(View v) - { - if (hasMoved) return false; - highBrightness(); - - remainingTime = totalTime; - vibrator.vibrate(250); - hasLongPressed = true; - isRunning = false; - invalidate(); - - return true; - } - }); - - setOnTouchListener(new View.OnTouchListener() - { - private float prevY; - private long prevTime; - - @Override - public boolean onTouch(View v, MotionEvent event) - { - int box = 20; - highBrightness(); - - switch (event.getAction() & MotionEvent.ACTION_MASK) - { - case MotionEvent.ACTION_DOWN: - prevY = event.getY(); - prevTime = remainingTime; - hasMoved = false; - hasLongPressed = false; - break; - - case MotionEvent.ACTION_MOVE: - float dy = (event.getY() - prevY) / box; - if (Math.abs(dy) < 1) break; - hasMoved = true; - - if (isRunning) break; - totalTime = Math.max(GRANULARITY, prevTime - (long) dy * GRANULARITY); - totalTime = (totalTime / GRANULARITY) * GRANULARITY; - remainingTime = totalTime; - - step = 1; - invalidate(); - break; - - case MotionEvent.ACTION_UP: - if (hasMoved) break; - if (hasLongPressed) break; - - isRunning = !isRunning; - - vibrator.vibrate(80); - invalidate(); - break; - } - - return false; - } - }); - - highBrightness(); } public void tick() { - Log.d(TAG, "tick()"); long currentTime = System.currentTimeMillis(); step = (step + 1) % 2; - if (brightnessCountdown-- == 0) lowBrightness(); - if (remainingTime <= 0) { isRunning = false; @@ -199,12 +145,10 @@ public class TimerView extends View implements AmbientModeListener PowerManager powerManager = (PowerManager) activity.getSystemService(Activity.POWER_SERVICE); - PowerManager.WakeLock mWakeLock = powerManager.newWakeLock( + PowerManager.WakeLock wakeLock = powerManager.newWakeLock( (PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MyWakelockTag"); - mWakeLock.acquire(); - - highBrightness(); + wakeLock.acquire(500); } } @@ -217,10 +161,15 @@ public class TimerView extends View implements AmbientModeListener return remainingTime % 60000; } - public long getRemainingTime() + public long getTime() { - if(!isRunning) return -1; - return remainingTime; + return totalTime; + } + + public void setTime(long totalTime) + { + this.totalTime = totalTime; + this.remainingTime = totalTime; } @Override @@ -243,20 +192,32 @@ public class TimerView extends View implements AmbientModeListener @Override protected void onDraw(Canvas canvas) { -// Log.d(TAG, "onDraw()"); paint = paintInteractive; - if (ambientMode) paint = paintAmbient; clearBackground(canvas); - drawOuterRing(canvas); - drawInnerRing(canvas); drawTimer(canvas); drawCurrentTime(canvas); } private void drawCurrentTime(Canvas canvas) { + if (ambientMode) paint.setColor(Color.WHITE); + else paint.setColor(primaryColor); + + Calendar c = Calendar.getInstance(); + int hour = c.get(Calendar.HOUR_OF_DAY); + int minutes = c.get(Calendar.MINUTE); + + if (ambientMode) + { + paint.setColor(Color.WHITE); + } + + paint.setTextSize(size * 0.08f); + canvas.drawText(String.format("%02d:%02d", hour, minutes), screenRect.centerX(), + screenRect.centerY() + (int) (fontHeight * 2.0), paint); + } private void drawTimer(Canvas canvas) @@ -270,7 +231,7 @@ public class TimerView extends View implements AmbientModeListener minutes = (long) (60 * Math.ceil(remainingTime / 1000 / 60.0)); paint.setTextSize(size * 0.25f); canvas.drawText(String.format("%d", minutes / 60), screenRect.centerX(), - screenRect.centerY() + (int) (fontHeight * 0.3), paint); + screenRect.centerY() + (int) (fontHeight * 0), paint); } else { @@ -289,13 +250,13 @@ public class TimerView extends View implements AmbientModeListener paint.setTextSize(size * 0.25f); canvas.drawText(String.format("%d", minutes / 60), screenRect.centerX() + minutesWidth / 2 - totalWidth / 2, - screenRect.centerY() + (int) (fontHeight * 0.3), paint); + screenRect.centerY() + (int) (fontHeight * 0), paint); paint.setTextSize(size * 0.15f); paint.setColor(secondaryColor); canvas.drawText(String.format("%02d", seconds), screenRect.centerX() + minutesWidth + secondsWidth / 2 + size * 0.025f - - totalWidth / 2, screenRect.centerY() + (int) (fontHeight * 0.3), + totalWidth / 2, screenRect.centerY() + (int) (fontHeight * 0), paint); } } @@ -306,59 +267,10 @@ public class TimerView extends View implements AmbientModeListener String text = "minutes"; if (minutes / 60 == 1) text = "minute"; - canvas.drawText(text, screenRect.centerX(), screenRect.centerY() + (int) (fontHeight * 1.0), + canvas.drawText(text, screenRect.centerX(), screenRect.centerY() + (int) (fontHeight * 0.7), paint); } - private void drawInnerRing(Canvas canvas) - { - int totalPieces = (int) Math.min(16, totalTime / 60000); - int remainingPieces = (int) Math.ceil(remainingTime / 60000.0); - - float pieceAngle = 360.0f / totalPieces; - float gap = 1.0f; - - RectF r = new RectF(screenRect); - r.inset(size * 0.1f, size * 0.1f); - - if (ambientMode) paint.setColor(Color.WHITE); - else paint.setColor(primaryColor); - - for (int i = 0; i < remainingPieces; i++) - canvas.drawArc(r, -90 - (i + 1) * pieceAngle, pieceAngle - gap, true, paint); - -// canvas.drawArc(r, -90.0f, -360.0f * (remainingTime / 1000) / (totalTime / 1000), true, paint); - - r.inset(size * 0.015f, size * 0.015f); - paint.setColor(backgroundColor); - canvas.drawArc(r, 0, 360, true, paint); - } - - private void drawOuterRing(Canvas canvas) - { - if (ambientMode) return; - - int totalPieces = 60 / 5; - float remainingPercentage = (remainingTime / 1000 % 60) / 60.0f; - int remainingPieces = (int) Math.ceil(remainingPercentage * totalPieces); - - if (remainingPieces == 0) remainingPieces = totalPieces; - - float pieceAngle = 360.0f / totalPieces; - float gap = 0.5f; - - RectF r = new RectF(screenRect); - r.inset(size * 0.075f, size * 0.075f); - - paint.setColor(tertiaryColor); - for (int i = 0; i < remainingPieces; i++) - canvas.drawArc(r, -90 - (i + 1) * pieceAngle, pieceAngle - gap, true, paint); - - r.inset(size * 0.015f, size * 0.015f); - paint.setColor(backgroundColor); - canvas.drawArc(r, 0, 360, true, paint); - } - private void clearBackground(Canvas canvas) { paint.setColor(backgroundColor); @@ -368,6 +280,8 @@ public class TimerView extends View implements AmbientModeListener @Override public void onEnterAmbient(Bundle ambientDetails) { + if (!isRunning) activity.finishAffinity(); + ambientMode = true; Log.d(TAG, "onEnterAmbient()"); } @@ -376,7 +290,6 @@ public class TimerView extends View implements AmbientModeListener public void onExitAmbient() { ambientMode = false; - highBrightness(); Log.d(TAG, "onExitAmbient()"); } @@ -386,50 +299,72 @@ public class TimerView extends View implements AmbientModeListener Log.d(TAG, "onUpdateAmbient()"); } - public static int mixColors(int color1, int color2, float amount) + class TouchListener implements View.OnTouchListener { - final byte ALPHA_CHANNEL = 24; - final byte RED_CHANNEL = 16; - final byte GREEN_CHANNEL = 8; - final byte BLUE_CHANNEL = 0; - - final float inverseAmount = 1.0f - amount; - - int a = ((int) (((float) (color1 >> ALPHA_CHANNEL & 0xff) * amount) + - ((float) (color2 >> ALPHA_CHANNEL & 0xff) * inverseAmount))) & 0xff; - int r = ((int) (((float) (color1 >> RED_CHANNEL & 0xff) * amount) + - ((float) (color2 >> RED_CHANNEL & 0xff) * inverseAmount))) & 0xff; - int g = ((int) (((float) (color1 >> GREEN_CHANNEL & 0xff) * amount) + - ((float) (color2 >> GREEN_CHANNEL & 0xff) * inverseAmount))) & 0xff; - int b = ((int) (((float) (color1 & 0xff) * amount) + - ((float) (color2 & 0xff) * inverseAmount))) & 0xff; - - return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL; - } + private float prevY; + private long prevTime; - public void changeBrightness(float brightness) - { - WindowManager.LayoutParams layout = activity.getWindow().getAttributes(); - layout.screenBrightness = brightness; - activity.getWindow().setAttributes(layout); - } + @Override + public boolean onTouch(View v, MotionEvent event) + { + int box = 20; - public void lowBrightness() - { - if (!isBright) return; + switch (event.getAction() & MotionEvent.ACTION_MASK) + { + case MotionEvent.ACTION_DOWN: + prevY = event.getY(); + prevTime = remainingTime; + hasMoved = false; + hasLongPressed = false; + break; + + case MotionEvent.ACTION_MOVE: + float dy = (event.getY() - prevY) / box; + if (Math.abs(dy) < 1) break; + hasMoved = true; + + if (isRunning) break; + + totalTime = Math.max(GRANULARITY, prevTime - (long) dy * GRANULARITY); + totalTime = (totalTime / GRANULARITY) * GRANULARITY; + remainingTime = totalTime; + + step = 1; + invalidate(); + break; - Log.d(TAG, "lowBrightness()"); - changeBrightness(0F); - isBright = false; + case MotionEvent.ACTION_UP: + if (hasMoved) break; + if (hasLongPressed) break; + + isRunning = !isRunning; + + vibrator.vibrate(80); + invalidate(); + break; + } + + return false; + } } - public void highBrightness() + class LongClickListener implements View.OnLongClickListener { - brightnessCountdown = BRIGHTNESS_LENGTH; - if (isBright) return; + @Override + public boolean onLongClick(View v) + { + if (hasMoved) return false; - Log.d(TAG, "highBrightness()"); - changeBrightness(0.8F); - isBright = true; + remainingTime = totalTime; + isRunning = false; + + vibrator.vibrate(250); + hasLongPressed = true; + + invalidate(); + + return true; + } } + } diff --git a/wear/src/main/res/layout/round_activity_main.xml b/wear/src/main/res/layout/round_activity_main.xml index 6793fce..4311ffd 100644 --- a/wear/src/main/res/layout/round_activity_main.xml +++ b/wear/src/main/res/layout/round_activity_main.xml @@ -6,7 +6,7 @@ tools:context=".MainActivity" tools:deviceIds="wear_round"> - + + + \ No newline at end of file diff --git a/wear/wear.iml b/wear/wear.iml deleted file mode 100644 index ea4785c..0000000 --- a/wear/wear.iml +++ /dev/null @@ -1,102 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file