Wear: move simple ui to layout

This commit is contained in:
Andries Smit 2021-12-29 20:06:43 +01:00
parent 51a88cff6c
commit 0d6a8aedf7
5 changed files with 167 additions and 93 deletions

View file

@ -10,6 +10,7 @@ import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.BatteryManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -25,6 +26,7 @@ import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.android.gms.wearable.DataMap; import com.google.android.gms.wearable.DataMap;
@ -40,7 +42,6 @@ import javax.inject.Inject;
import dagger.android.AndroidInjection; import dagger.android.AndroidInjection;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.complications.BaseComplicationProviderService;
import info.nightscout.androidaps.data.ListenerService; import info.nightscout.androidaps.data.ListenerService;
import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.interaction.utils.Persistence; import info.nightscout.androidaps.interaction.utils.Persistence;
@ -59,7 +60,6 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
@Inject Persistence persistence; @Inject Persistence persistence;
public final static IntentFilter INTENT_FILTER; public final static IntentFilter INTENT_FILTER;
public static final long[] vibratePattern = {0, 400, 300, 400, 300, 400};
static { static {
INTENT_FILTER = new IntentFilter(); INTENT_FILTER = new IntentFilter();
@ -70,10 +70,11 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
public final Point displaySize = new Point(); public final Point displaySize = new Point();
public TextView mTime, mHour, mMinute, mSgv, mDirection, mTimestamp, mUploaderBattery, mRigBattery, mDelta, mAvgDelta, mStatus, mBasalRate, mIOB1, mIOB2, mCOB1, mCOB2, mBgi, mLoop, mDay, mDayName, mMonth, isAAPSv2, mHighLight, mLowLight; public TextView mTime, mHour, mMinute, mSgv, mDirection, mTimestamp, mUploaderBattery, mRigBattery, mDelta, mAvgDelta, mStatus, mBasalRate, mIOB1, mIOB2, mCOB1, mCOB2, mBgi, mLoop, mDay, mDayName, mMonth, isAAPSv2, mHighLight, mLowLight;
public TextView mSimpleSvg, mSimpleDirection, mSimpleTime;
public ImageView mGlucoseDial, mDeltaGauge, mHourHand, mMinuteHand; public ImageView mGlucoseDial, mDeltaGauge, mHourHand, mMinuteHand;
public View mSimpleUi;
public RelativeLayout mRelativeLayout; public RelativeLayout mRelativeLayout;
public LinearLayout mLinearLayout, mLinearLayout2, mLinearLayout3, mDate, mChartTap, public LinearLayout mLinearLayout, mLinearLayout2, mDate, mChartTap, mMainMenuTap;
mMainMenuTap;
public int ageLevel = 1; public int ageLevel = 1;
public int loopLevel = 1; public int loopLevel = 1;
public int highColor = Color.YELLOW; public int highColor = Color.YELLOW;
@ -101,6 +102,8 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
protected SharedPreferences sharedPrefs; protected SharedPreferences sharedPrefs;
private LocalBroadcastManager localBroadcastManager; private LocalBroadcastManager localBroadcastManager;
private MessageReceiver messageReceiver; private MessageReceiver messageReceiver;
private BroadcastReceiver batteryReceiver;
protected boolean isCharging = false;
@Override @Override
public void onCreate() { public void onCreate() {
@ -122,6 +125,26 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
sharedPrefs.registerOnSharedPreferenceChangeListener(this); sharedPrefs.registerOnSharedPreferenceChangeListener(this);
persistence.turnOff(); persistence.turnOff();
setupBatteryReceiver();
}
private void setupBatteryReceiver() {
if (sharedPrefs.getBoolean("simplify_ui_charging", false)) {
IntentFilter intentBatteryFilter = new IntentFilter();
intentBatteryFilter.addAction(BatteryManager.ACTION_CHARGING);
intentBatteryFilter.addAction(BatteryManager.ACTION_DISCHARGING);
isCharging = checkIsCharging();
batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
isCharging = checkIsCharging();
setDataFields();
invalidate();
}
};
registerReceiver(batteryReceiver, intentBatteryFilter);
}
} }
@Override @Override
@ -168,7 +191,6 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
mHighLight = stub.findViewById(R.id.highLight); mHighLight = stub.findViewById(R.id.highLight);
mLowLight = stub.findViewById(R.id.lowLight); mLowLight = stub.findViewById(R.id.lowLight);
mRelativeLayout = stub.findViewById(R.id.main_layout); mRelativeLayout = stub.findViewById(R.id.main_layout);
mLinearLayout3 = stub.findViewById(R.id.primary_layout);
mLinearLayout = stub.findViewById(R.id.secondary_layout); mLinearLayout = stub.findViewById(R.id.secondary_layout);
mLinearLayout2 = stub.findViewById(R.id.tertiary_layout); mLinearLayout2 = stub.findViewById(R.id.tertiary_layout);
mGlucoseDial = stub.findViewById(R.id.glucose_dial); mGlucoseDial = stub.findViewById(R.id.glucose_dial);
@ -178,8 +200,11 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
mChartTap = stub.findViewById(R.id.chart_zoom_tap); mChartTap = stub.findViewById(R.id.chart_zoom_tap);
mMainMenuTap = stub.findViewById(R.id.main_menu_tap); mMainMenuTap = stub.findViewById(R.id.main_menu_tap);
chart = stub.findViewById(R.id.chart); chart = stub.findViewById(R.id.chart);
mSimpleUi = stub.findViewById(R.id.simple_ui);
mSimpleSvg = stub.findViewById(R.id.simple_sgv);
mSimpleDirection = stub.findViewById(R.id.simple_direction);
mSimpleTime = stub.findViewById(R.id.simple_watch_time);
layoutSet = true; layoutSet = true;
setDataFields(); setDataFields();
setColor(); setColor();
} }
@ -219,6 +244,9 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
if (sharedPrefs != null) { if (sharedPrefs != null) {
sharedPrefs.unregisterOnSharedPreferenceChangeListener(this); sharedPrefs.unregisterOnSharedPreferenceChangeListener(this);
} }
if (batteryReceiver != null) {
unregisterReceiver(batteryReceiver);
}
super.onDestroy(); super.onDestroy();
} }
@ -258,8 +286,16 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
} }
} }
private boolean checkIsCharging() {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, iFilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
return status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
}
private void checkVibrateHourly(WatchFaceTime oldTime, WatchFaceTime newTime) { private void checkVibrateHourly(WatchFaceTime oldTime, WatchFaceTime newTime) {
Boolean hourlyVibratePref = sharedPrefs.getBoolean("vibrate_Hourly", false); boolean hourlyVibratePref = sharedPrefs.getBoolean("vibrate_Hourly", false);
if (hourlyVibratePref && layoutSet && newTime.hasHourChanged(oldTime)) { if (hourlyVibratePref && layoutSet && newTime.hasHourChanged(oldTime)) {
Log.i("hourlyVibratePref", "true --> " + newTime.toString()); Log.i("hourlyVibratePref", "true --> " + newTime.toString());
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
@ -450,6 +486,37 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
mLoop.setVisibility(View.GONE); mLoop.setVisibility(View.GONE);
} }
} }
setDataFieldsSimpleUi();
}
void setDataFieldsSimpleUi() {
if (sharedPrefs.getBoolean("simplify_ui_charging", false) && isCharging) {
mSimpleUi.setVisibility(View.VISIBLE);
mSimpleSvg.setText(rawData.sSgv);
if (ageLevel() <= 0) {
mSimpleSvg.setPaintFlags(mSimpleSvg.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
mSimpleSvg.setPaintFlags(mSimpleSvg.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
if (rawData.sgvLevel == 1) {
mSimpleSvg.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor));
mSimpleDirection.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_highColor));
} else if (rawData.sgvLevel == 0) {
mSimpleSvg.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor));
mSimpleDirection.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_midColor));
} else if (rawData.sgvLevel == -1) {
mSimpleSvg.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor));
mSimpleDirection.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.dark_lowColor));
}
mSimpleDirection.setText(rawData.sDirection+"\uFE0E");
final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(BaseWatchFace.this);
mSimpleTime.setText(timeFormat.format(System.currentTimeMillis()));
} else {
mSimpleUi.setVisibility(View.GONE);
}
} }
public void setDateAndTime() { public void setDateAndTime() {
@ -528,7 +595,7 @@ public abstract class BaseWatchFace extends WatchFace implements SharedPreferenc
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
setupBatteryReceiver();
if ("delta_granularity".equals(key)) { if ("delta_granularity".equals(key)) {
ListenerService.requestData(this); ListenerService.requestData(this);
} }

View file

@ -11,6 +11,7 @@ import androidx.core.content.ContextCompat;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.support.wearable.watchface.WatchFaceStyle; import android.support.wearable.watchface.WatchFaceStyle;
import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -32,27 +33,9 @@ public class Home2 extends BaseWatchFace {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.activity_home_2, null); layoutView = inflater.inflate(R.layout.activity_home_2, null);
IntentFilter intentBatteryFilter = new IntentFilter();
intentBatteryFilter.addAction(BatteryManager.ACTION_CHARGING);
intentBatteryFilter.addAction(BatteryManager.ACTION_DISCHARGING);
registerReceiver(batteryReceiver, intentBatteryFilter);
performViewSetup(); performViewSetup();
} }
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setDataFields();
invalidate();
}
};
@Override
public void onDestroy() {
unregisterReceiver(batteryReceiver);
super.onDestroy();
}
@Override @Override
protected void onTapCommand(int tapType, int x, int y, long eventTime) { protected void onTapCommand(int tapType, int x, int y, long eventTime) {
@ -286,58 +269,4 @@ public class Home2 extends BaseWatchFace {
} }
} }
@Override
public void setDataFields() {
super.setDataFields();
if (sharedPrefs.getBoolean("simplify_ui_charging", false) && isCharging()) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
0.5f
);
mLinearLayout3.setLayoutParams(param);
mLinearLayout3.setWeightSum(0.7f);
mLinearLayout.setVisibility(View.GONE);
mLoop.setVisibility(View.INVISIBLE);
chart.setVisibility(View.GONE);
mIOB1.setVisibility(View.GONE);
mIOB2.setVisibility(View.GONE);
mCOB1.setVisibility(View.GONE);
mCOB2.setVisibility(View.GONE);
mTimestamp.setVisibility(View.GONE);
mTime.setTextSize(35);
mDirection.setTextSize(35);
mSgv.setTextSize(50);
} else {
TypedValue outValue = new TypedValue();
getResources().getValue(R.dimen.home2_primary_layout_height, outValue, true);
float layoutHeight = outValue.getFloat();
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
layoutHeight
);
mLinearLayout3.setLayoutParams(param);
mLinearLayout.setVisibility(View.VISIBLE);
mLoop.setVisibility(View.VISIBLE);
chart.setVisibility(View.VISIBLE);
mIOB1.setVisibility(View.VISIBLE);
mIOB2.setVisibility(View.VISIBLE);
mCOB1.setVisibility(View.VISIBLE);
mCOB2.setVisibility(View.VISIBLE);
mTimestamp.setVisibility(View.VISIBLE);
mDirection.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.home2_direction_text_size));
mSgv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.home2_sgv_text_size));
mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.home2_time_text_size));
}
}
private boolean isCharging() {
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, iFilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
return status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
}
} }

View file

@ -20,7 +20,7 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="0px" android:layout_height="0px"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_weight="@dimen/home2_primary_layout_height" android:layout_weight="0.27"
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:orientation="horizontal" android:orientation="horizontal"
android:textAlignment="center"> android:textAlignment="center">
@ -50,7 +50,7 @@
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="---" android:text="---"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
android:textSize="@dimen/home2_sgv_text_size" /> android:textSize="38sp" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -70,7 +70,7 @@
android:text="--" android:text="--"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
android:textSize="@dimen/home2_direction_text_size" android:textSize="22sp"
android:textStyle="bold" /> android:textStyle="bold" />
<TextView <TextView
@ -275,7 +275,7 @@
android:text="12:00" android:text="12:00"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
android:textSize="@dimen/home2_time_text_size" /> android:textSize="30sp" />
<LinearLayout <LinearLayout
android:id="@+id/date_time" android:id="@+id/date_time"
@ -370,4 +370,9 @@
</LinearLayout> </LinearLayout>
<include
android:id="@+id/simple_ui"
layout="@layout/simple_ui"
android:visibility="gone" />
</RelativeLayout> </RelativeLayout>

View file

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:textAlignment="center">
<TextView
android:id="@+id/simple_sgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="-2dp"
android:gravity="bottom|end"
android:text="---"
android:textColor="@color/white"
android:textSize="50sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:baselineAligned="false"
android:gravity="center_horizontal"
android:orientation="vertical"
android:textAlignment="center"
android:weightSum="1">
<TextView
android:id="@+id/simple_direction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="-2dp"
android:layout_marginBottom="-5dp"
android:gravity="center_horizontal|bottom"
android:text="--"
android:textColor="@color/white"
android:textSize="35sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center_horizontal">
<TextView
android:id="@+id/simple_watch_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="5dp"
android:text="12:00"
android:textColor="#FFFFFF"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="home2_primary_layout_height">0.27</dimen>
<dimen name="home2_sgv_text_size">38sp</dimen>
<dimen name="home2_direction_text_size">22sp</dimen>
<dimen name="home2_time_text_size">30sp</dimen>
</resources>