新版界面数据帧

This commit is contained in:
luozhenxue@yztiot 2025-05-16 09:38:34 +08:00
parent 938228bc65
commit 2a84a59edb
22 changed files with 473 additions and 462 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -7,6 +7,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;
import java.util.Calendar;
@ -15,7 +16,13 @@ public class BootCompleteReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// 读取文件并设置AlarmManager
setAlarms(context);
SharedPreferences prefs = context.getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
int switchValue = prefs.getInt(MainActivity.SWITCH_KEY,-1);
Log.d("BootCompleteReceiver","BootCompleteReceiver: "+switchValue);
Toast.makeText(context, "switchValue: "+switchValue, Toast.LENGTH_SHORT).show();
if (switchValue == 1){
setAlarms(context);
}
}
}
@ -26,6 +33,9 @@ public class BootCompleteReceiver extends BroadcastReceiver {
int endHour = prefs.getInt(MainActivity.END_HOUR_KEY, -1);
int endMinute = prefs.getInt(MainActivity.END_MINUTE_KEY, -1);
if (startHour != -1 && startMinute != -1 && endHour != -1 && endMinute != -1) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

View File

@ -1,201 +0,0 @@
package com.yztiot.streetlamp;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ColorModeAdapter extends RecyclerView.Adapter<ColorModeAdapter.ViewHolder> {
private List<String> titles;
private int selectedPosition = -1;
public ColorModeAdapter(List<String> titles) {
this.titles = titles;
}
private OnSelectionChangedListener selectionChangedListener;
public static class ViewHolder extends RecyclerView.ViewHolder {
View rootView;
TextView status;
ImageView lightModeBackground;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView;
status = itemView.findViewById(R.id.color_mode_status);
lightModeBackground = itemView.findViewById(R.id.light_mode_background);
}
}
@Override
public ColorModeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.color_mode, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ColorModeAdapter.ViewHolder holder, int position) {
String colorName = titles.get(position);
holder.status.setText(colorName);
// 设置颜色圆圈背景
View colorShow = holder.rootView.findViewById(R.id.color_show);
colorShow.setBackground(getColorDrawableByName(colorName));
// 设置选中样式
if (position == selectedPosition) {
holder.lightModeBackground.setBackgroundResource(R.drawable.btn_yellow);
} else {
holder.lightModeBackground.setBackgroundResource(R.drawable.btn_transparent);
}
holder.rootView.setOnClickListener(v -> {
int currentPos = holder.getAdapterPosition();
if (currentPos == RecyclerView.NO_POSITION) return;
int oldPos = selectedPosition;
selectedPosition = currentPos;
notifyItemChanged(oldPos);
notifyItemChanged(selectedPosition);
Log.d("ColorMode", "选中:" + colorName);
if (selectionChangedListener != null) {
selectionChangedListener.onSelectionChanged(selectedPosition);
}
});
}
private GradientDrawable getColorDrawableByName(String colorName) {
// 创建一个 GradientDrawable 对象
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.OVAL); // 明确设置形状为圆形
switch (colorName) {
case "红黄":
case "Red-Yellow":
drawable.setColors(new int[]{0xCCFF0000, 0xCCFFFF00});
break;
case "红紫":
case "Red-Purple":
drawable.setColors(new int[]{0xCCFF0000, 0xCC800080});
break;
case "红青":
case "Red-Cyan":
drawable.setColors(new int[]{0xCCFF0000, 0xCC00FFFF});
break;
case "绿青":
case "Green-Cyan":
drawable.setColors(new int[]{0xCC00FF00, 0xCC00FFFF});
break;
case "绿蓝":
case "Green-Blue":
drawable.setColors(new int[]{0xCC00FF00, 0xCC0000FF});
break;
case "绿黄":
case "Green-Yellow":
drawable.setColors(new int[]{0xCC00FF00, 0xCCFFFF00});
break;
case "绿紫":
case "Green-Purple":
drawable.setColors(new int[]{0xCC00FF00, 0xCC800080});
break;
case "黄紫":
case "Yellow-Purple":
drawable.setColors(new int[]{0xCCFFFF00, 0xCC800080});
break;
case "黄青":
case "Yellow-Cyan":
drawable.setColors(new int[]{0xCCFFFF00, 0xCC00FFFF});
break;
case "紫青":
case "Purple-Cyan":
drawable.setColors(new int[]{0xCC800080, 0xCC00FFFF});
break;
case "红绿蓝":
case "Red-Green-Blue":
drawable.setColors(new int[]{0xCCFF0000, 0xCC00FF00, 0xCC0000FF});
break;
case "紫黄青":
case "Purple-Yellow-Cyan":
drawable.setColors(new int[]{0xCC800080, 0xCCFFFF00, 0xCC00FFFF});
break;
case "红蓝":
case "Red-Blue":
drawable.setColors(new int[]{0xCCFF0000, 0xCC0000FF});
break;
case "Red-Green":
case "红绿":
drawable.setColors(new int[]{0xCCFF0000, 0xCC00FF00});
break;
case "Red":
case "":
drawable.setColor(0xCCFF0000);
break;
case "Green":
case "绿":
drawable.setColor(0xCC00FF00);
break;
case "Blue":
case "":
drawable.setColor(0xCC0000FF);
break;
case "White":
case "":
drawable.setColor(0xCCFFFFFF);
break;
case "Black":
case "":
drawable.setColors(new int[]{ 0xCC575757,0xCC000000});
break;
case "":
case "Purple":
drawable.setColor(0xCC800080);
break;
case "Yellow":
case "":
drawable.setColor(0xCCFFFF00);
break;
case "Cyan":
case "":
drawable.setColor(0xCC00FFFF);
break;
case "":
case "off":
default:
drawable.setColor(0x00444444);
break;
}
return drawable;
}
public interface OnSelectionChangedListener {
void onSelectionChanged(int selectedPosition);
}
public void setOnSelectionChangedListener(OnSelectionChangedListener listener) {
this.selectionChangedListener = listener;
}
@Override
public int getItemCount() {
return titles.size();
}
public int getSelectedPosition() {
return selectedPosition;
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
}

View File

@ -0,0 +1,53 @@
package com.yztiot.streetlamp;
public class LightMode {
private String modeName;
private int colorResId;
private int color1ResId;
private int color2ResId;
private int color3ResId;
private int index;
public LightMode(String modeName) {
this.modeName = modeName;
this.colorResId = 0;
}
public LightMode(String modeName, int colorResId) {
this.modeName = modeName;
this.colorResId = colorResId;
}
public LightMode(String modeName, int color1ResId, int color2ResId) {
this.modeName = modeName;
this.color1ResId = color1ResId;
this.color2ResId = color2ResId;
}
public LightMode(String modeName, int color1ResId, int color2ResId, int color3ResId) {
this.modeName = modeName;
this.color1ResId = color1ResId;
this.color2ResId = color2ResId;
this.color3ResId = color3ResId;
}
public String getModeName() {
return modeName;
}
public int getColorResId() {
return colorResId;
}
public int getColor1ResId() {
return color1ResId;
}
public int getColor2ResId() {
return color2ResId;
}
public int getColor3ResId() {
return color3ResId;
}
}

View File

@ -1,92 +1,138 @@
package com.yztiot.streetlamp;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class LightModeAdapter extends RecyclerView.Adapter<LightModeAdapter.ViewHolder> {
private List<String> titles;
public class LightModeAdapter extends RecyclerView.Adapter<LightModeAdapter.LightModeViewHolder> {
private List<LightMode> lightModes;
private int selectedPosition = -1;
private OnSelectionChangedListener selectionChangedListener;
private int containerId;
public LightModeAdapter(List<String> titles) {
this.titles = titles;
private OnLightModeSelectedListener listener;
public LightModeAdapter(List<LightMode> lightModes, int containerId, OnLightModeSelectedListener listener) {
this.lightModes = lightModes;
this.containerId = containerId;
this.listener = listener;
}
public interface OnLightModeSelectedListener {
void onLightModeSelected(LightMode lightMode, int containerId, int position, StringBuilder frameBuilder);
void onFrameBuilt(String frame);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
View rootView;
TextView title, color;
ImageView lightModeBackground;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView;
title = itemView.findViewById(R.id.light_mode_title);
color = itemView.findViewById(R.id.light_mode_color);
lightModeBackground = itemView.findViewById(R.id.light_mode_background);
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
notifyDataSetChanged(); // 刷新视图
}
@Override
public LightModeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.light_mode, parent, false);
return new ViewHolder(view);
public LightModeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.color_mode, parent, false);
return new LightModeViewHolder(view);
}
@Override
public void onBindViewHolder(LightModeAdapter.ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.color.setText(R.string.off);
public void onBindViewHolder(LightModeViewHolder holder, int position) {
LightMode lightMode = lightModes.get(position);
holder.bind(lightMode, position == selectedPosition);
if (position == selectedPosition) {
holder.lightModeBackground.setBackgroundResource(R.drawable.light_mode_on);
} else {
holder.lightModeBackground.setBackgroundResource(R.drawable.light_mode_off);
}
holder.itemView.setOnClickListener(v -> {
// 记录当前选中的位置
int oldPosition = selectedPosition;
selectedPosition = position; // 更新为当前选中的位置
holder.rootView.setOnClickListener(v -> {
int currentPos = holder.getAdapterPosition();
if (currentPos == RecyclerView.NO_POSITION) return;
int oldPos = selectedPosition;
selectedPosition = currentPos;
notifyItemChanged(oldPos);
// 更新显示
notifyItemChanged(oldPosition);
notifyItemChanged(selectedPosition);
Log.d("LightMode", "选中:" + titles.get(currentPos));
if (selectionChangedListener != null) {
selectionChangedListener.onSelectionChanged(selectedPosition);
if (listener != null) {
listener.onLightModeSelected(lightMode, containerId, position, new StringBuilder()); // 传递选中的模式
}
Log.d("LightModeAdapter", "Clicked containerId: " + containerId + ", position: " + position);
});
}
public interface OnSelectionChangedListener {
void onSelectionChanged(int selectedPosition);
}
public void setOnSelectionChangedListener(OnSelectionChangedListener listener) {
this.selectionChangedListener = listener;
}
@Override
public int getItemCount() {
return titles.size();
return lightModes.size();
}
public int getSelectedPosition() {
return selectedPosition;
public static class LightModeViewHolder extends RecyclerView.ViewHolder {
private TextView modeStatus;
private View colorShow, colorMode;
public LightModeViewHolder(View itemView) {
super(itemView);
modeStatus = itemView.findViewById(R.id.color_mode_status);
colorShow = itemView.findViewById(R.id.color_show);
colorMode = itemView.findViewById(R.id.color_mode);
}
public void bind(LightMode lightMode, boolean isSelected) {
itemView.setTag(lightMode);
modeStatus.setText(lightMode.getModeName());
// 设置选中状态样式
if (isSelected) {
colorMode.setBackgroundResource(R.drawable.light_mode_on);
modeStatus.setTextColor(Color.BLACK);
} else {
colorMode.setBackgroundResource(R.drawable.light_mode_off); // 你需要创建这个背景
modeStatus.setTextColor(Color.GRAY);
}
// 渐变色设置与之前一样
if (lightMode.getColorResId() != 0) {
int color = ContextCompat.getColor(itemView.getContext(), lightMode.getColorResId());
GradientDrawable circleDrawable = new GradientDrawable();
circleDrawable.setColor(color);
circleDrawable.setShape(GradientDrawable.OVAL);
colorShow.setBackground(circleDrawable);
} else if (lightMode.getColor1ResId() != 0 && lightMode.getColor2ResId() != 0) {
int color1 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor1ResId());
int color2 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor2ResId());
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[]{color1, color2});
gradientDrawable.setShape(GradientDrawable.OVAL);
colorShow.setBackground(gradientDrawable);
} else if (lightMode.getColor1ResId() != 0 && lightMode.getColor2ResId() != 0 && lightMode.getColor3ResId() != 0) {
int color1 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor1ResId());
int color2 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor2ResId());
int color3 = ContextCompat.getColor(itemView.getContext(), lightMode.getColor3ResId());
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[]{color1, color2, color3});
gradientDrawable.setShape(GradientDrawable.OVAL);
colorShow.setBackground(gradientDrawable);
} else {
int[] rainbowColors = {
Color.parseColor("#FF6B6B"),
Color.parseColor("#FFA94D"),
Color.parseColor("#FFD43B"),
Color.parseColor("#69DB7C"),
Color.parseColor("#38D9A9"),
Color.parseColor("#4DABF7"),
Color.parseColor("#9775FA")
};
GradientDrawable rainbowDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
rainbowColors);
rainbowDrawable.setShape(GradientDrawable.OVAL);
colorShow.setBackground(rainbowDrawable);
}
}
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
}
}

View File

@ -2,7 +2,6 @@ package com.yztiot.streetlamp;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
@ -12,7 +11,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
@ -23,7 +21,6 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
@ -33,35 +30,40 @@ import androidx.recyclerview.widget.RecyclerView;
import com.yztiot.demo.yztiotManager;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private RecyclerView lightModeView,colorModeView;
private ImageView lightModeImage,lowerLamplight,upperLamplight;
private ImageButton btnSettings,btnSpeed,btnTiming;
private Button btnUpLight, btnBlowLight, btnSend,loginPhoto,btnLogin;
private Spinner spinner;
private Button btnSend,loginPhoto,btnLogin;
private LightModeAdapter lightModeAdapter;
private ColorModeAdapter colorModeAdapter;
private Spinner spinner;
private boolean isSerialOpen = false;
private int upperLight = -1, upperColor = -1;
private int lowerLight = -1, lowerColor = -1;
public boolean isSelectingUpper = true;
private int upperLight = 01, upperColor = -1;
private int lowerLight = 02, lowerColor = -1;
public static final String PREFS_NAME = "password_prefs";
private static final String SUPER_ADMIN_PASSWORD_KEY = "super_admin_password";
private static final String USER_PASSWORD_KEY = "user_password";
private String superAdminPassword = "654321";
private int saveSwitch;
private String frame;
private String userPassword = "000000";
public static final String START_HOUR_KEY = "start_hour";
public static final String START_MINUTE_KEY = "start_minute";
public static final String END_HOUR_KEY = "end_hour";
public static final String SWITCH_KEY = "switch";
public static final String FRAME_KEY = "frame";
public static final String END_MINUTE_KEY = "end_minute";
private View loginInputLayout;
private EditText loginInput;
private FrameLayout screenLayout;
private boolean isLoginInputVisible = false;
private RecyclerView lightModeContainer,colorModeContainer;
private int selectedLightModeUpper;
private int selectedLightModeLower;
public static com.yztiot.demo.yztiotManager yztiotManager = new yztiotManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -76,57 +78,34 @@ public class MainActivity extends AppCompatActivity {
openSerial();
initializePasswords();
loadPasswords();
setAlarmManager();
if (saveSwitch == 0){
cancelAlarmManager();
} else if (saveSwitch == 1){
setAlarmManager();
}
initializeSelection();
}
private void initializeSelection() {
int lightModePosition = Integer.parseInt(frame.substring(6, 8), 16); // 子节目1
int colorModePosition = Integer.parseInt(frame.substring(10, 12), 16); // 子节目2
// 设置 lightModeContainer 的选中位置
if (colorModeContainer.getAdapter() != null) {
LightModeAdapter lightModeAdapter = (LightModeAdapter) lightModeContainer.getAdapter();
lightModeAdapter.setSelectedPosition(lightModePosition );
lightModeAdapter.notifyDataSetChanged();
}
// 设置 colorModeContainer 的选中位置
if (colorModeContainer.getAdapter() != null) {
LightModeAdapter colorModeAdapter = (LightModeAdapter) colorModeContainer.getAdapter();
colorModeAdapter.setSelectedPosition(colorModePosition); // 索引从0开始
colorModeAdapter.notifyDataSetChanged();
}
Log.d("lightModeAdapter", "lightModeAdapter: " + lightModeAdapter + " colorModeContainer :" + colorModeContainer);
}
private void onClickListener(){
btnUpLight.setOnClickListener(v -> {
lightModeImage.setBackgroundResource(R.drawable.upper_light);
upperLamplight.setVisibility(View.VISIBLE);
lowerLamplight.setVisibility(View.GONE);
isSelectingUpper = true;
upperLight = lightModeAdapter.getSelectedPosition();
upperColor = colorModeAdapter.getSelectedPosition();
lightModeAdapter.setSelectedPosition(upperLight);
colorModeAdapter.setSelectedPosition(upperColor);
if (upperLight >= 0 && upperColor >= 0) {
Log.d("UpperProgram", String.format("light: %02X, color: %02X, upperProgram: %02X %02X", upperLight, upperColor, upperLight, upperColor));
}
});
btnBlowLight.setOnClickListener(v -> {
lightModeImage.setBackgroundResource(R.drawable.lower_light);
upperLamplight.setVisibility(View.GONE);
lowerLamplight.setVisibility(View.VISIBLE);
isSelectingUpper = false;
lowerLight = lightModeAdapter.getSelectedPosition();
lowerColor = colorModeAdapter.getSelectedPosition();
lightModeAdapter.setSelectedPosition(lowerLight);
colorModeAdapter.setSelectedPosition(lowerColor);
if (lowerLight >= 0 && lowerColor >= 0) {
Log.d("LowerProgram", String.format("light: %02X, color: %02X, lowerProgram: %02X %02X", lowerLight, lowerColor, lowerLight, lowerColor));
}
});
lightModeAdapter.setOnSelectionChangedListener(position -> {
if (isSelectingUpper) {
upperLight = position;
} else {
lowerLight = position;
}
});
colorModeAdapter.setOnSelectionChangedListener(position -> {
if (isSelectingUpper) {
upperColor = position;
} else {
lowerColor = position;
}
});
btnSend.setOnClickListener(v -> {
SerialManage.getInstance().init(new SerialInter() {
@ -150,6 +129,25 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, R.string.send_successfully, Toast.LENGTH_SHORT).show();
});
btnTiming.setOnLongClickListener(v -> {
new AlertDialog.Builder(MainActivity.this, R.style.BlueAlertDialog)
.setTitle(R.string.timer_switch)
.setMessage(R.string.confirm_open_timer) // 提示信息
.setNegativeButton(R.string.cancel, (dialog, which) -> {
setAlarmManager();
Toast.makeText(MainActivity.this, R.string.timer_cancelled, Toast.LENGTH_SHORT).show();
})
.setPositiveButton(R.string.confirm, (dialog, which) -> {
// 用户确认打开定时开关
Toast.makeText(MainActivity.this, R.string.timer_opened, Toast.LENGTH_SHORT).show();
// 例如设置定时器
cancelAlarmManager();
})
.show();
return true; // 返回 true 表示事件已处理
});
btnTiming.setOnClickListener(v -> {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
@ -271,8 +269,29 @@ public class MainActivity extends AppCompatActivity {
}
loginInput.setText("");
});
}
}
private void cancelAlarmManager() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent startIntent = new Intent(this, TimingReceiver.class);
startIntent.setAction("com.yztiot.streetlamp.START_ALARM");
PendingIntent startPendingIntent = PendingIntent.getBroadcast(this, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent endIntent = new Intent(this, TimingReceiver.class);
endIntent.setAction("com.yztiot.streetlamp.END_ALARM");
PendingIntent endPendingIntent = PendingIntent.getBroadcast(this, 1, endIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (alarmManager != null) {
alarmManager.cancel(startPendingIntent);
alarmManager.cancel(endPendingIntent);
Log.d("MainActivity", "Alarms canceled");
}
// 更换按钮背景图
btnTiming.setBackgroundResource(R.drawable.btn_timing_off);
saveSwitch(SWITCH_KEY,0);
}
private void setAlarmManager() {
// 获取保存的时间
SharedPreferences prefs = getSharedPreferences(MainActivity.PREFS_NAME, MODE_PRIVATE);
@ -317,10 +336,19 @@ public class MainActivity extends AppCompatActivity {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, endCalendar.getTimeInMillis(), endPendingIntent);
Log.d("MainActivity", "End alarm set for: " + endCalendar.getTime());
btnTiming.setBackgroundResource(R.drawable.btn_timing_on);
saveSwitch(SWITCH_KEY,1);
} else {
Log.d("MainActivity", "No valid time settings found");
}
}
private void saveSwitch(String key, int value) {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, value);
editor.apply();
Log.d("SwitchSave", key + ": " + value);
}
private void saveTime(String key, int value) {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
@ -328,6 +356,13 @@ public class MainActivity extends AppCompatActivity {
editor.apply();
Log.d("TimeSave", key + ": " + value);
}
private void saveFrame(String key, String value) {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.apply();
Log.d("saveFrame", key + ": " + value);
}
private void savePasswords() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
@ -340,29 +375,24 @@ public class MainActivity extends AppCompatActivity {
byte FH = 0x55;
byte channel = (byte) (spinner.getSelectedItemPosition() + 1);
byte upperHigh = (byte) ((upperLight + 1) & 0xFF);
byte upperHigh = (byte) ((upperLight ) & 0xFF);
byte upperLow = (byte) (upperColor & 0xFF);
byte lowerHigh = (byte) ((lowerLight + 1) & 0xFF);
byte lowerHigh = (byte) ((lowerLight) & 0xFF);
byte lowerLow = (byte) (lowerColor & 0xFF);
byte speed = 0x32;
byte extra = 0x01; // 新增字节
byte[] frame = new byte[9];
byte[] frame = new byte[7];
frame[0] = FH;
frame[1] = channel;
frame[2] = upperHigh;
frame[3] = upperLow;
frame[4] = lowerHigh;
frame[5] = lowerLow;
frame[6] = speed;
frame[7] = extra;
int sum = 0;
for (int i = 0; i < 8; i++) {
for (int i = 0; i < 6; i++) {
sum += (frame[i] & 0xFF);
}
frame[8] = (byte) (sum & 0xFF);
frame[6] = (byte) (sum & 0xFF);
StringBuilder sb = new StringBuilder();
for (byte b : frame) {
@ -377,6 +407,7 @@ public class MainActivity extends AppCompatActivity {
byte[] bytesToSend = hexStringToByteArray(hexToSend);
String hexString = bytesToHex(bytesToSend);
SerialManage.getInstance().send(hexString);
saveFrame(FRAME_KEY,hexString);
} else {
Toast.makeText(this, R.string.port_manager_not_initialized, Toast.LENGTH_SHORT).show();
}
@ -454,15 +485,17 @@ public class MainActivity extends AppCompatActivity {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
superAdminPassword = prefs.getString(SUPER_ADMIN_PASSWORD_KEY, "654321");
userPassword = prefs.getString(USER_PASSWORD_KEY, "000000");
saveSwitch = prefs.getInt(SWITCH_KEY, 0);
frame = prefs.getString(FRAME_KEY,"");
Log.d("PasswordLoad", "Super Admin Password: " + superAdminPassword);
Log.d("PasswordLoad", "User Password: " + userPassword);
Log.d("PasswordLoad", "saveSwitch: " + saveSwitch);
Log.d("PasswordLoad", "saveSwitch: " + frame);
}
private void findId(){
spinner = findViewById(R.id.ttys_spinner);
btnUpLight = findViewById(R.id.btn_up_light);
btnSend = findViewById(R.id.btn_send);
btnBlowLight = findViewById(R.id.btn_blow_light);
lightModeImage = findViewById(R.id.light_mode_image);
lowerLamplight = findViewById(R.id.lower_lamplight);
upperLamplight = findViewById(R.id.upper_lamplight);
@ -474,20 +507,10 @@ public class MainActivity extends AppCompatActivity {
loginPhoto = findViewById(R.id.login_photo);
screenLayout = findViewById(R.id.screen_layout);
btnLogin = findViewById(R.id.btn_login);
colorModeContainer = findViewById(R.id.color_mode_container);
lightModeContainer = findViewById(R.id.light_mode_container);
}
private void layout(){
lightModeView = findViewById(R.id.light_mode_container);
lightModeView.setLayoutManager(new LinearLayoutManager(this));
lightModeAdapter = new LightModeAdapter(Arrays.asList(getResources().getStringArray(R.array.light_modes)));
lightModeView.setAdapter(lightModeAdapter);
colorModeView = findViewById(R.id.color_mode_container);
colorModeView.setLayoutManager(new LinearLayoutManager(this));
colorModeAdapter = new ColorModeAdapter(Arrays.asList(getResources().getStringArray(R.array.color_modes)));
colorModeView.setAdapter(colorModeAdapter);
spinner = findViewById(R.id.ttys_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
@ -496,12 +519,98 @@ public class MainActivity extends AppCompatActivity {
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
int red = R.color.red;
int green = R.color.green;
int blue = R.color.blue;
int white = R.color.white;
int black = R.color.black;
int purple = R.color.purple;
int yellow = R.color.yellow;
int cyan = R.color.cyan;
int transparent = R.color.transparent;
List<LightMode> colorModes = new ArrayList<>();
// 单色常亮 (8个)
colorModes.add(new LightMode(getString(R.string.off), transparent));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), red));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), green));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), blue));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), white));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), black));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), purple));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), yellow));
colorModes.add(new LightMode(getString(R.string.solid_color_constant), cyan));
// 单色呼吸渐变 (7个)
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), red));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), green));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), blue));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), white));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), purple));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), yellow));
colorModes.add(new LightMode(getString(R.string.solid_color_breathing), cyan));
// 双色交替渐变 (9个)
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), red, yellow));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), red, purple));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), red, cyan));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), green, cyan));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), green, yellow));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), green, purple));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), yellow, purple));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), yellow, cyan));
colorModes.add(new LightMode(getString(R.string.dual_color_alternating), purple, cyan));
// 三色渐变 (2个)
colorModes.add(new LightMode(getString(R.string.tricolor_gradient),red, green, blue));
colorModes.add(new LightMode(getString(R.string.tricolor_gradient), purple, yellow, cyan));
// 其他模式 (各1个)
colorModes.add(new LightMode(getString(R.string.seven_color_gradient)));
colorModes.add(new LightMode(getString(R.string.seven_color_jump)));
colorModes.add(new LightMode(getString(R.string.colorful_trail_run)));
colorModes.add(new LightMode(getString(R.string.colorful_color_chase)));
while (colorModes.size() < 30) {
colorModes.addAll(colorModes);
}
colorModes = colorModes.subList(0, 30);
lightModeContainer.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
colorModeContainer.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
LightModeAdapter.OnLightModeSelectedListener listener = new LightModeAdapter.OnLightModeSelectedListener() {
@Override
public void onLightModeSelected(LightMode lightMode, int containerId, int position, StringBuilder frameBuilder) {
if (containerId == 1) {
upperColor = position; // 存储上部选中的模式
Log.d("MainActivity", "Upper Light Mode Selected: " + lightMode.getModeName() + " at position " + position);
} else if (containerId == 2) {
lowerColor = position; // 存储下部选中的模式
Log.d("MainActivity", "Lower Light Mode Selected: " + selectedLightModeLower + " at position " + position);
}
Log.d("MainActivity", "upperColor " + upperColor + " lowerColor " + lowerColor);
}
@Override
public void onFrameBuilt(String frame) {
// 可以根据需要处理数据帧
}
};
lightModeContainer.setAdapter(new LightModeAdapter(colorModes, 01, listener));
colorModeContainer.setAdapter(new LightModeAdapter(colorModes, 02, listener));
}
private Runnable hideNavBarRunnable = new Runnable() {
@Override
public void run() {
hideNavigationBar();
handler.postDelayed(this, 1000); // 每秒检查一次
handler.postDelayed(this, 1000);
}
};
@Override

View File

@ -24,16 +24,17 @@ public class TimingReceiver extends BroadcastReceiver {
int startMinute = prefs.getInt(MainActivity.START_MINUTE_KEY, -1);
int endHour = prefs.getInt(MainActivity.END_HOUR_KEY, -1);
int endMinute = prefs.getInt(MainActivity.END_MINUTE_KEY, -1);
String frame = prefs.getString(MainActivity.FRAME_KEY,"");
if ("com.yztiot.streetlamp.START_ALARM".equals(intent.getAction())) {
if (startHour == currentHour && startMinute == currentMinute) {
Log.d("TimingCheck", "开始时间到达: " + currentHour + ":" + currentMinute);
send("55010101020232018F");
Log.d("TimingCheck", "开始时间到达: " + currentHour + ":" + currentMinute+"frame: "+frame);
send(frame);
}
} else if ("com.yztiot.streetlamp.END_ALARM".equals(intent.getAction())) {
if (endHour == currentHour && endMinute == currentMinute) {
Log.d("TimingCheck", "结束时间到达: " + currentHour + ":" + currentMinute);
send("550101010202320290");
Log.d("TimingCheck", "结束时间到达: " + currentHour + ":" + currentMinute+"frame: "+frame);
send("55010100020059");
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 480 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 474 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -1,4 +1,4 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#515576" /> <!-- 默认颜色,可被代码中覆盖 -->
<solid android:color="#515576" />
</shape>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 474 B

After

Width:  |  Height:  |  Size: 328 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -48,7 +48,7 @@
android:layout_marginTop="@dimen/y3"
android:layout_marginLeft="@dimen/y11">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="@dimen/y74"
android:layout_width="@dimen/y80"
android:layout_height="@dimen/y205"
android:orientation="vertical"
android:layout_marginTop="@dimen/y29"
@ -56,11 +56,11 @@
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="@dimen/y80"
android:layout_width="@dimen/y85"
android:layout_height="@dimen/y205"
android:orientation="vertical"
android:layout_marginTop="@dimen/y29"
android:layout_marginLeft="@dimen/y5"
android:layout_marginLeft="@dimen/y2"
android:id="@+id/color_mode_container">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
@ -94,6 +94,7 @@
android:background="@drawable/btn_send"/>
</LinearLayout>
<ImageView
android:visibility="gone"
android:id="@+id/upper_lamplight"
android:layout_width="237.1px"
android:layout_height="158.2px"

View File

@ -1,23 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="177px"
android:layout_height="60px"
android:id="@+id/color_mode"
android:background="@drawable/light_mode_off"
android:layout_marginTop="@dimen/y1">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black">
<ImageView
android:id="@+id/light_mode_background"
android:layout_width="@dimen/y84"
android:layout_height="@dimen/y22"
android:background="@drawable/btn_yellow"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="@dimen/y15"
android:layout_marginLeft="@dimen/y5"
android:layout_gravity="left|center">
<View
android:id="@+id/color_show"
@ -26,10 +22,10 @@
android:background="@drawable/circle_shape" />
<TextView
android:id="@+id/color_mode_status"
android:layout_width="wrap_content"
android:layout_width="127px"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/y5"
android:text=""
android:text=""
android:textSize="@dimen/y7"
android:textColor="@color/white"/>
</LinearLayout>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
<Switch
android:id="@+id/enableSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="启用定时"
android:textColor="#0055DE"/>
</LinearLayout>

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/light_mode"
android:layout_marginTop="@dimen/y1">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/light_mode_background"
android:layout_width="@dimen/y74"
android:layout_height="@dimen/y22"
android:background="@drawable/light_mode_off"/>
<TextView
android:id="@+id/light_mode_title"
android:layout_width="@dimen/y65"
android:layout_height="wrap_content"
android:text="aaaa"
android:textSize="@dimen/y5"
android:layout_gravity="left|top"
android:layout_margin="@dimen/y3"
android:textColor="@color/white"/>
<TextView
android:visibility="gone"
android:id="@+id/light_mode_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:textSize="@dimen/y5"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/y3"
android:textColor="#FFC300"/>
</FrameLayout>
</LinearLayout>

View File

@ -1,7 +1,17 @@
<resources>
<string name="app_name">LED Remote Control System</string>
<string-array name="light_modes">
<string-array name="up_light_modes">
<item>@string/solid_color_constant</item>
<item>@string/solid_color_breathing</item>
<item>@string/dual_color_alternating</item>
<item>@string/tricolor_gradient</item>
<item>@string/seven_color_gradient</item>
<item>@string/seven_color_jump</item>
<item>@string/colorful_trail_run</item>
<item>@string/colorful_color_chase</item>
</string-array>
<string-array name="blower_light_modes">
<item>@string/solid_color_constant</item>
<item>@string/solid_color_breathing</item>
<item>@string/dual_color_alternating</item>
@ -12,31 +22,31 @@
<item>@string/colorful_color_chase</item>
</string-array>
<string-array name="color_modes">
<item>@string/off</item>
<item>@string/red</item>
<item>@string/green</item>
<item>@string/blue</item>
<item>@string/white</item>
<item>@string/black</item>
<item>@string/purple</item>
<item>@string/red_blue</item>
<item>@string/yellow</item>
<item>@string/red_green</item>
<item>@string/cyan</item>
<item>@string/green_blue</item>
<item>@string/red_yellow</item>
<item>@string/red_purple</item>
<item>@string/red_cyan</item>
<item>@string/green_cyan</item>
<item>@string/green_yellow</item>
<item>@string/green_purple</item>
<item>@string/yellow_purple</item>
<item>@string/yellow_cyan</item>
<item>@string/purple_cyan</item>
<item>@string/red_green_blue</item>
<item>@string/purple_yellow_cyan</item>
</string-array>
<!-- <string-array name="color_modes">-->
<!-- <item>@string/off</item>-->
<!-- <item>@string/red</item>-->
<!-- <item>@string/green</item>-->
<!-- <item>@string/blue</item>-->
<!-- <item>@string/white</item>-->
<!-- <item>@string/black</item>-->
<!-- <item>@string/purple</item>-->
<!-- <item>@string/red_blue</item>-->
<!-- <item>@string/yellow</item>-->
<!-- <item>@string/red_green</item>-->
<!-- <item>@string/cyan</item>-->
<!-- <item>@string/green_blue</item>-->
<!-- <item>@string/red_yellow</item>-->
<!-- <item>@string/red_purple</item>-->
<!-- <item>@string/red_cyan</item>-->
<!-- <item>@string/green_cyan</item>-->
<!-- <item>@string/green_yellow</item>-->
<!-- <item>@string/green_purple</item>-->
<!-- <item>@string/yellow_purple</item>-->
<!-- <item>@string/yellow_cyan</item>-->
<!-- <item>@string/purple_cyan</item>-->
<!-- <item>@string/red_green_blue</item>-->
<!-- <item>@string/purple_yellow_cyan</item>-->
<!-- </string-array>-->
<string name="solid_color_constant">Solid Color Constant Light</string>
<string name="solid_color_breathing">Solid Color Breathing Gradient</string>
@ -90,4 +100,8 @@
<string name="modify_password">Modify the user password</string>
<string name="password_modified_successfully">The password has been modified successfully. The new password is: </string>
<string name="password_cannot_be_empty">The password cannot be empty!</string>
<string name="timer_switch">Timing switch</string>
<string name="confirm_open_timer">Whether to turn on the timer switch?</string>
<string name="timer_opened">The timing switch has been turned on</string>
<string name="timer_cancelled">The timer switch has been cancelled</string>
</resources>

View File

@ -2,7 +2,13 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FF1807</color>
<color name="red">#FF0000</color>
<color name="green">#00FF00</color>
<color name="blue">#0000FF</color>
<color name="purple">#800080</color>
<color name="yellow">#FFFF00</color>
<color name="cyan">#00FFFF</color>
<color name="transparent">#0000FFFF</color>
<color name="colorPrimary">#2196F3</color>
<color name="colorPrimaryDark">#1976D2</color>
<color name="colorAccent">#2196F3</color>

View File

@ -2,7 +2,7 @@
<!-- App Name -->
<string name="app_name">LED远程控制系统</string>
<string-array name="light_modes">
<string-array name="up_light_modes">
<item>@string/solid_color_constant</item>
<item>@string/solid_color_breathing</item>
<item>@string/dual_color_alternating</item>
@ -12,32 +12,15 @@
<item>@string/colorful_trail_run</item>
<item>@string/colorful_color_chase</item>
</string-array>
<!-- Colors -->
<string-array name="color_modes">
<item>@string/off</item>
<item>@string/red</item>
<item>@string/green</item>
<item>@string/blue</item>
<item>@string/white</item>
<item>@string/black</item>
<item>@string/purple</item>
<item>@string/red_blue</item>
<item>@string/yellow</item>
<item>@string/red_green</item>
<item>@string/cyan</item>
<item>@string/green_blue</item>
<item>@string/red_yellow</item>
<item>@string/red_purple</item>
<item>@string/red_cyan</item>
<item>@string/green_cyan</item>
<item>@string/green_yellow</item>
<item>@string/green_purple</item>
<item>@string/yellow_purple</item>
<item>@string/yellow_cyan</item>
<item>@string/purple_cyan</item>
<item>@string/red_green_blue</item>
<item>@string/purple_yellow_cyan</item>
<string-array name="blower_light_modes">
<item>@string/solid_color_constant</item>
<item>@string/solid_color_breathing</item>
<item>@string/dual_color_alternating</item>
<item>@string/tricolor_gradient</item>
<item>@string/seven_color_gradient</item>
<item>@string/seven_color_jump</item>
<item>@string/colorful_trail_run</item>
<item>@string/colorful_color_chase</item>
</string-array>
<string name="solid_color_constant">单色常亮</string>
@ -86,10 +69,14 @@
<string name="enter_su_password">请输入超级管理员密码</string>
<string name="authentication">身份验证</string>
<string name="enter_new_password">请输入新用户密码</string>
<string name="confirm">确认</string>
<string name="cancel">取消</string>
<string name="speed">速度</string>
<string name="modify_password">修改用户密码</string>
<string name="password_modified_successfully">密码修改成功,新密码为: </string>
<string name="password_cannot_be_empty">密码不能为空!</string>
<string name="timer_switch">定时开关</string>
<string name="confirm_open_timer">是否关闭定时开关?</string>
<string name="timer_opened">定时开关已打开</string>
<string name="timer_cancelled">定时开关已取消</string>
<string name="confirm">确认</string>
<string name="cancel">取消</string>
</resources>