Skip to content

Commit

Permalink
add a receiver to reset time after the user lock the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
lypeer committed Sep 15, 2016
1 parent b1229f5 commit fc3f1a3
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand All @@ -11,6 +11,8 @@
<item name="lyColor0">#000000</item>
<item name="lyColor1">#000000</item>
<item name="lyColor2">#000000</item>
<item name="lyColor3">#000000</item>
<item name="lyColorColon">#000000</item>
<item name="lyThickness">20</item>
<item name="lyColonThickness">1</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.annotation.DimenRes;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -211,7 +212,7 @@ private void updateClock(TimeUpdateEvent event) {
updateImageView(mIvSecSingleDigits, mResList.get(event.getSecSingleDigits()));
mSecSingleDigits = event.getSecSingleDigits();
}
updateImageView(mIvColon, R.drawable.svg_colon);
updateImageView(mIvColon, R.drawable.animated_colon);
}

/**
Expand All @@ -229,19 +230,33 @@ private void updateImageView(ImageView target, int animTo) {
}
Drawable drawable = mContext.getDrawable(animTo);
if (drawable == null) {
Log.e(mContext.getPackageCodePath(), "ResId is wrong .");
return;
}

drawable.canApplyTheme();
drawable.applyTheme(wrapper.getTheme());
target.setImageDrawable(drawable);
drawable.applyTheme(wrapper.getTheme());

if (drawable instanceof AnimatedVectorDrawable) {
final AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) drawable;

if (mHourTenDigits != -1 &&
mHourSingleDigits != -1 &&
mMinTenDigits != -1 &&
mMinSingleDigits != -1 &&
mSecTenDigits != -1) {
animatedVectorDrawable.start();
return;
}
//todo there exists a bug in XiaoMi phone : when the time is 00:00 ,
//todo 11:11, 22:22(there are some same numbers) , only the first can work correctly
animatedVectorDrawable.start();
//todo So I do this to avoid that . But this way is so fool .
target.post(new Runnable() {
@Override
public void run() {
animatedVectorDrawable.start();
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.lypeer.googleioclock.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;

Expand Down Expand Up @@ -41,6 +44,14 @@ public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mReceiver , filter);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
beginClock();
Expand Down Expand Up @@ -135,11 +146,20 @@ private void initTime() {
mSecTenDigits = seconds / 10;
}

// Monitoring the screen on to reset time
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
initTime();
}
};

@Override
public void onDestroy() {
super.onDestroy();
if (mTimer != null) {
mTimer.cancel();
}
unregisterReceiver(mReceiver);
}
}
8 changes: 8 additions & 0 deletions googleioclock/src/main/res/drawable/animated_colon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/svg_colon">
<!--In fact , no animation should be here , but this file is a must-->
<target
android:name="colon"
android:animation="@animator/anim_line"/>
</animated-vector>
4 changes: 2 additions & 2 deletions googleioclock/src/main/res/drawable/svg_7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
android:strokeWidth="?attr/lyThickness" />
<path
android:pathData="@string/path_data_7"
android:strokeColor="?attr/lyColor1"
android:strokeColor="?attr/lyColor2"
android:trimPathStart="0.5"
android:name="seven2"
android:trimPathEnd="0.75"
android:strokeWidth="?attr/lyThickness" />
<path
android:pathData="@string/path_data_7"
android:strokeColor="?attr/lyColor1"
android:strokeColor="?attr/lyColor3"
android:trimPathStart="0.75"
android:name="seven3"
android:trimPathEnd="1"
Expand Down
1 change: 1 addition & 0 deletions googleioclock/src/main/res/drawable/svg_colon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<path
android:strokeColor="?attr/lyColorColon"
android:strokeWidth="?attr/lyColonThickness"
android:name="colon"
android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2z
M12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>
4 changes: 2 additions & 2 deletions googleioclock/src/main/res/layout/view_google_clock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="@string/cd_iv_hour_ten_digits"
android:src="@drawable/svg_0"/>
android:src="@drawable/svg_1"/>

<View
android:id="@+id/view_divider_horizontal_0"
Expand Down Expand Up @@ -59,7 +59,7 @@
<View
android:layout_width="match_parent"
android:id="@+id/view_divider_vertical"
android:layout_height="4dp"/>
android:layout_height="16dp"/>

<LinearLayout
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions googleioclock/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
<!--todo In fact , thickness should be a float value , but Android don't allow us to define float in xml .-->
<attr name="lyThickness" format="integer"/>
<attr name="lyColonThickness" format="integer"/>
<!-- <attr name="lyTransformDuration" format="integer"/>
<attr name="lyLineDuration" format="integer"/>-->
</declare-styleable>
</resources>
4 changes: 3 additions & 1 deletion googleioclock/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="lyThickness_4">4</integer>
<integer name="lyThickness_4">8</integer>
<integer name="lyThickness_1">1</integer>
<!--<integer name="lyDuration_4000">4000</integer>
<integer name="lyDuration_500">500</integer>-->
</resources>
2 changes: 2 additions & 0 deletions googleioclock/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
<item name="lyColorColon">@color/colorDarkBlue</item>
<item name="lyThickness">@integer/lyThickness_4</item>
<item name="lyColonThickness">@integer/lyThickness_1</item>
<!-- <item name="lyTransformDuration">@integer/lyDuration_500</item>
<item name="lyLineDuration">@integer/lyDuration_4000</item>-->
</style>
</resources>

0 comments on commit fc3f1a3

Please sign in to comment.