Commit 6249ed85 by wjg

add update

parent b3bead92
......@@ -37,7 +37,8 @@
android:name=".view.LauncherActivity"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="landscape">
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
......@@ -21,8 +21,8 @@ public abstract class NetworkHandler extends Handler {
String msgText = String.format("请求:%s, 返回状态:%s, 返回内容:%s", url, msg.what, msg.obj);
Utils.i(AppConfig.TAG_NETWORK, msgText);
}
if (msg == null || msg.what == 0) {
return;
}
// if (msg == null || msg.what == 0) {
// return;
// }
}
}
......@@ -8,33 +8,27 @@ import androidx.annotation.NonNull;
import com.ihaoin.hooloo.device.util.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SettingsQueue extends ArrayList<Boolean> {
private List trigger = Arrays.asList(true, false);
// private List trigger = Arrays.asList(true, false, true, false, true, false, true);
public class SettingsQueue extends ArrayList<Long> {
private Integer SIZE = 7;
private Context context;
private View oneView;
private View zeroView;
private View view;
public SettingsQueue(@NonNull Context context, @NonNull View oneView, @NonNull View zeroView) {
public SettingsQueue(@NonNull Context context, @NonNull View view) {
super();
this.context = context;
this.oneView = oneView;
this.zeroView = zeroView;
this.view = view;
this.oneView.setOnClickListener(v -> add(true));
this.zeroView.setOnClickListener(v -> add(false));
this.view.setOnClickListener(v -> add(System.currentTimeMillis()));
}
@Override
public boolean add(Boolean o) {
if (size() >= trigger.size()) {
public boolean add(Long l) {
if (size() >= SIZE) {
remove(0);
}
boolean result = super.add(o);
if (size() >= trigger.size()) {
boolean result = super.add(l);
if (size() >= SIZE) {
ifMatch();
}
return result;
......@@ -42,14 +36,17 @@ public class SettingsQueue extends ArrayList<Boolean> {
private void ifMatch() {
boolean matched = true;
for (int i = 0; i < trigger.size(); i++) {
if (trigger.get(i) != get(i)) {
for (int i = 0; i < SIZE; i++) {
if (i == 0) {
continue;
}
if (get(i) - get(i - 1) > 1000) {
matched = false;
break;
}
}
if (matched) {
Utils.openAppSettings(context);
Utils.showPwdDialog(context);
clear();
}
}
......
package com.ihaoin.hooloo.device.component;
import android.annotation.SuppressLint;
import android.content.pm.PackageInfo;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
......@@ -11,12 +12,21 @@ import com.ihaoin.hooloo.device.HLApplication;
import com.ihaoin.hooloo.device.data.vo.CheckUpdateVo;
import com.ihaoin.hooloo.device.network.HttpUtil;
import com.ihaoin.hooloo.device.util.ApkUtils;
import com.ihaoin.hooloo.device.util.JsonUtils;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;
import java.io.File;
@SuppressLint("HandlerLeak")
public class UpdateService extends Thread {
// @Nullable
// @Override
// public IBinder onBind(Intent intent) {
// checkUpdate();
// return null;
// }
@Override
public void run() {
checkUpdate();
......@@ -45,34 +55,34 @@ public class UpdateService extends Thread {
@Override
public void handleMessage(@NonNull Message msg) {
try {
CheckUpdateVo updateVo = new CheckUpdateVo();
updateVo.setUrl("https://testapi.pecktoy.com/files/upload_test/signed-v10.apk");
updateVo.setVersion("1.0.10");
updateVo.setVersionCode(10);
// Object obj = msg.obj;
// if (msg.what == 0 || obj == null) {
// Utils.i("check update error");
// return;
// }
// CheckUpdateVo updateVo = JsonUtils.readValue(obj, CheckUpdateVo.class);
// if (updateVo.getVersionCode() <= 0) {
// Utils.i("version code is 0");
// return;
// }
// if (StringUtils.isEmpty(updateVo.getUrl())) {
// Utils.i("url is null");
// return;
// }
// if (StringUtils.isEmpty(updateVo.getVersion())) {
// Utils.i("version name is null");
// return;
// }
//
// PackageInfo packageInfo = Utils.getPackageInfo();
// if (updateVo.getVersionCode() <= packageInfo.versionCode) {
// Utils.i(String.format("dont update, current: %s, update: %s", packageInfo.versionCode, updateVo.getVersionCode()));
// return;
// }
// CheckUpdateVo updateVo = new CheckUpdateVo();
// updateVo.setUrl("https://testapi.pecktoy.com/files/upload_test/signed-v10.apk");
// updateVo.setVersion("1.0.10");
// updateVo.setVersionCode(10);
Object obj = msg.obj;
if (msg.what == 0 || obj == null) {
Utils.i("check update error");
return;
}
CheckUpdateVo updateVo = JsonUtils.readValue(obj, CheckUpdateVo.class);
if (updateVo.getVersionCode() == null || updateVo.getVersionCode() <= 0) {
Utils.i("version code is null");
return;
}
if (StringUtils.isEmpty(updateVo.getUrl())) {
Utils.i("url is null");
return;
}
if (StringUtils.isEmpty(updateVo.getVersion())) {
Utils.i("version name is null");
return;
}
PackageInfo packageInfo = Utils.getPackageInfo();
if (updateVo.getVersionCode() <= packageInfo.versionCode) {
Utils.i(String.format("dont update, current: %s, update: %s", packageInfo.versionCode, updateVo.getVersionCode()));
return;
}
startDownload(updateVo);
} catch (Exception e) {
......
......@@ -3,6 +3,7 @@ package com.ihaoin.hooloo.device.config;
public class Base {
public static final Integer TRUE = 1;
public static final Integer FALSE = 0;
public static final Integer DELETE = 2;
public static final String PUSH_DEFAULT_EXTRA = "PUSH_DEFAULT_EXTRA";
public static final String PUSH_ACTION_ORDER_STATE_CHANGED = "ORDER_STATE_CHANGED";
......
......@@ -6,10 +6,30 @@ public class CheckUpdate implements Serializable {
private String machineCode;
private String appName;
private String type;
private Integer versionCode;
private String versionName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMachineCode() {
return machineCode;
}
......
......@@ -20,6 +20,10 @@ public class GoodsRule implements Serializable {
*/
private Integer isDefault;
/**
* 是否删除
*/
private Integer isDelete;
/**
* 是否推荐
*/
private Integer isRecommend;
......@@ -79,4 +83,12 @@ public class GoodsRule implements Serializable {
public void setIsRecommend(Integer isRecommend) {
this.isRecommend = isRecommend;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
}
......@@ -12,6 +12,8 @@ public class Sku implements Serializable {
private Integer state;
/** 默认状态:0-否,1-是 */
private Integer isDefault;
/** 是否删除:0-否,1-物理删除,2-手动删除 */
private Integer isDelete;
/** 原价 */
private BigDecimal price;
/** 折扣价 */
......@@ -66,4 +68,12 @@ public class Sku implements Serializable {
public void setRules(List<SkuRule> rules) {
this.rules = rules;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
}
......@@ -14,5 +14,5 @@ public class HttpParams {
/** 保存推送ID */
public static final String POST_REGISTRATION_ID = AppConfig.HOST + "/application/jgRegister?machineCode=%s&registerId=%s";
/** 检查更新 */
public static final String POST_CHECK_UPDATE = "https://brain.ihaoin.com/mc/upgrade";
public static final String POST_CHECK_UPDATE = "http://brain.ihaoin.com/mc/upgrade";
}
package com.ihaoin.hooloo.device.network;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.fasterxml.jackson.databind.JsonNode;
import com.ihaoin.hooloo.device.HLApplication;
import com.ihaoin.hooloo.device.config.AppConfig;
import com.ihaoin.hooloo.device.data.po.CheckUpdate;
import com.ihaoin.hooloo.device.util.JsonUtils;
import com.ihaoin.hooloo.device.util.SharedPreferencesUtils;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
......@@ -65,11 +69,15 @@ public class HttpUtil {
});
}
private static OkHttpClient getHttpClient() {
return new OkHttpClient.Builder().callTimeout(15, TimeUnit.SECONDS).readTimeout(15, TimeUnit.SECONDS).build();
}
public static void post(String url, String body, Handler handler) {
if (body == null) {
body = "";
}
OkHttpClient client = new OkHttpClient();
OkHttpClient client = getHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), body);
Request request = new Request.Builder().url(url).post(requestBody).build();
client.newCall(request).enqueue(new Callback() {
......@@ -86,7 +94,7 @@ public class HttpUtil {
}
public static void get(String url, Handler handler) {
OkHttpClient client = new OkHttpClient();
OkHttpClient client = getHttpClient();
Request request = new Request.Builder().url(url).get().build();
client.newCall(request).enqueue(new Callback() {
@Override
......@@ -122,9 +130,16 @@ public class HttpUtil {
onFailure(handler, call, new BizException("返回body为空"));
return;
}
if (jsonNode.get("code").asInt() != 200) {
onFailure(handler, call, new BizException(jsonNode.get("msg").asText()));
return;
if (jsonNode.get("request_id") == null) {
if (jsonNode.get("code").asInt() != 200) {
onFailure(handler, call, new BizException(jsonNode.get("msg").asText()));
return;
}
} else {
if (jsonNode.get("code").asInt() != 0) {
onFailure(handler, call, new BizException(responseBody));
return;
}
}
if (handler == null) {
String msgText = String.format("请求:%s, 返回状态:%s, 返回内容:%s", call.request().url(), 1, responseBody);
......@@ -179,11 +194,17 @@ public class HttpUtil {
}
public static void checkUpdate(Handler handler) {
PackageInfo packageInfo = Utils.getPackageInfo();
CheckUpdate checkUpdate = new CheckUpdate();
checkUpdate.setMachineCode(AppConfig.MACHINE_CODE);
checkUpdate.setVersionCode(Utils.getPackageInfo().versionCode);
checkUpdate.setVersionName(Utils.getPackageInfo().versionName);
post(HttpParams.POST_CHECK_UPDATE, JsonUtils.toString(checkUpdate), handler);
checkUpdate.setAppName(packageInfo.packageName);
checkUpdate.setVersionCode(packageInfo.versionCode);
checkUpdate.setVersionName(packageInfo.versionName);
checkUpdate.setType(SharedPreferencesUtils.getScreenNo(HLApplication.SELF));
String content = JsonUtils.toString(checkUpdate).replaceAll("\"", "'");
String params = String.format("{\"params\": \"%s\"}", content);
post(HttpParams.POST_CHECK_UPDATE, params, handler);
}
// public static String getMacAddress() {
......
package com.ihaoin.hooloo.device.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
......@@ -14,6 +16,8 @@ import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
......@@ -37,6 +41,7 @@ import com.ihaoin.hooloo.device.data.vo.Sku;
import com.ihaoin.hooloo.device.view.ClearDialog;
import com.ihaoin.hooloo.device.view.ConfirmOrderDialog;
import com.ihaoin.hooloo.device.view.GoodsDetailDialog;
import com.ihaoin.hooloo.device.view.PwdDialog;
import com.ihaoin.hooloo.device.view.SettingsActivity;
import com.ihaoin.hooloo.device.view.TestDialog;
import com.ihaoin.hooloo.device.view.TipsDialog;
......@@ -127,6 +132,11 @@ public class Utils {
dialog.show();
}
public static void showPwdDialog(Context context) {
PwdDialog dialog = new PwdDialog(context);
dialog.show();
}
public static void showConfirmOrder(Context context, List<TrolleyGoods> trolleyGoods) {
ConfirmOrderDialog dialog = new ConfirmOrderDialog(context);
dialog.setGoods(trolleyGoods);
......
......@@ -50,6 +50,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
public class GoodsDetailDialog extends Dialog {
......@@ -76,10 +77,23 @@ public class GoodsDetailDialog extends Dialog {
private View layoutButns;
private List<View> introViews;
private List<Integer> availableRuleId;
public GoodsDetailDialog(@NonNull Context context, Goods goods) {
super(context);
this.mGoods = goods;
// this.mGoods.getSkus().forEach(sku -> sku.getRules().forEach(rule -> {
// if (rule.getRuleName().equals("热")) {
// sku.setIsDelete(2);
// }
// }));
// 处理售罄的SKU和规格选项
processSelloutRules();
// 处理删除的SKU和规格选项
processDeleteRules();
}
@Override
......@@ -199,9 +213,6 @@ public class GoodsDetailDialog extends Dialog {
ViewGroup layoutItem = findViewById(R.id.layout_item);
Utils.setTouchDelegate(layoutItem, Arrays.asList(butnAdd, butnSubtract));
// 处理售罄的SKU和规格选项
processSpecRules();
buildSpecs();
buildDetails();
......@@ -372,10 +383,81 @@ public class GoodsDetailDialog extends Dialog {
Utils.fastPushBroadcast(getContext(), Base.PUSH_ACTION_SKU_STATE_CHANGED, JsonUtils.toString(changedVo));
}
}
processSpecRules();
processSelloutRules();
buildSpecs();
}
private void buildSpecs() {
layoutSpecs.removeAllViews();
if (CollectionUtils.isEmpty(mGoods.getSpecs())) {
return;
}
mGoods.getSpecs().forEach(spec -> {
if (CollectionUtils.isEmpty(spec.getRules())) {
return;
}
View specView = getLayoutInflater().inflate(R.layout.item_spec, null);
TextView txtName = specView.findViewById(R.id.txt_name);
AutoWrapLayout layoutRules = specView.findViewById(R.id.layout_rules);
txtName.setText(spec.getSpecName());
AtomicBoolean checkNextFlag = new AtomicBoolean(false);
spec.getRules().forEach(rule -> {
AutoWrapLayout.LayoutParams layoutParams = new AutoWrapLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
layoutParams.rightMargin = Utils.getDimens(getContext(), R.dimen.padding5);
View ruleView = getLayoutInflater().inflate(R.layout.item_spec_rule, null);
ruleView.setTag(rule);
TextView button = ruleView.findViewById(R.id.butn_rule);
button.setId(rule.getRuleId());
button.setText(rule.getRuleName());
if (Base.TRUE.equals(rule.getIsRecommend())) {
ruleView.findViewById(R.id.txt_recommend).setVisibility(View.VISIBLE);
}
if (Base.DELETE.equals(rule.getIsDelete())) {
if (Base.TRUE.equals(rule.getIsDefault())) { // 如果默认选项售罄,则选中第一个未售罄的选项
checkNextFlag.set(true);
}
return;
} else if (SkuState.isSellin(rule.getState())) {
if (Base.TRUE.equals(rule.getIsDefault())) {
ruleView.setSelected(true);
button.setSelected(true);
}
ruleView.setOnClickListener(v -> {
clearSelected(layoutRules);
ruleView.setSelected(true);
button.setSelected(true);
changeSkuWithRule();
});
} else {
ruleView.setEnabled(false);
button.setEnabled(false);
if (Base.TRUE.equals(rule.getIsDefault())) { // 如果默认选项售罄,则选中第一个未售罄的选项
checkNextFlag.set(true);
}
}
layoutRules.addView(ruleView, layoutParams);
});
// 如果默认选项售罄,则选中第一个未售罄的选项
if (checkNextFlag.get() && layoutRules.getChildCount() > 0) {
for (int i = 0; i < layoutRules.getChildCount(); i++) {
ViewGroup ruleView = (ViewGroup) layoutRules.getChildAt(i);
if (ruleView.isEnabled()) {
ruleView.callOnClick();
break;
}
}
}
layoutSpecs.addView(specView);
// layoutRules.setOnCheckedChangeListener((group, checkedId) -> changeSkuWithRule());
});
changeSkuWithRule();
}
public void prepareBuyNow() {
if (mSku == null) {
Utils.showToast(getContext(), R.string.not_found_goods);
......@@ -425,83 +507,6 @@ public class GoodsDetailDialog extends Dialog {
txtCount.setText(String.valueOf(mCount));
}
private void buildSpecs() {
layoutSpecs.removeAllViews();
if (CollectionUtils.isEmpty(mGoods.getSpecs())) {
return;
}
mGoods.getSpecs().forEach(spec -> {
if (CollectionUtils.isEmpty(spec.getRules())) {
return;
}
// GoodsRule r1 = new GoodsRule();
// GoodsRule r2 = new GoodsRule();
// GoodsRule r3 = new GoodsRule();
// r1.setRuleId(101);
// r1.setRuleName("101");
// r1.setIsDefault(0);
// r1.setIsRecommend(0);
// r1.setState(1);
// r1.setPrice(new BigDecimal("1"));
//
// r2.setRuleId(102);
// r2.setRuleName("102");
// r2.setIsDefault(0);
// r2.setIsRecommend(0);
// r2.setState(1);
// r2.setPrice(new BigDecimal("2"));
//
// r3.setRuleId(103);
// r3.setRuleName("103");
// r3.setIsDefault(0);
// r3.setIsRecommend(0);
// r3.setState(1);
// r3.setPrice(new BigDecimal("3"));
//
// spec.getRules().add(r1);
// spec.getRules().add(r2);
// spec.getRules().add(r3);
View specView = getLayoutInflater().inflate(R.layout.item_spec, null);
TextView txtName = specView.findViewById(R.id.txt_name);
AutoWrapLayout layoutRules = specView.findViewById(R.id.layout_rules);
txtName.setText(spec.getSpecName());
spec.getRules().forEach(rule -> {
AutoWrapLayout.LayoutParams layoutParams = new AutoWrapLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
layoutParams.rightMargin = Utils.getDimens(getContext(), R.dimen.padding5);
View ruleView = getLayoutInflater().inflate(R.layout.item_spec_rule, null);
ruleView.setTag(rule);
TextView button = ruleView.findViewById(R.id.butn_rule);
button.setId(rule.getRuleId());
button.setText(rule.getRuleName());
if (Base.TRUE.equals(rule.getIsRecommend())) {
ruleView.findViewById(R.id.txt_recommend).setVisibility(View.VISIBLE);
}
if (!SkuState.isSellin(rule.getState())) {
button.setEnabled(false);
} else {
if (Base.TRUE.equals(rule.getIsDefault())) {
button.setSelected(true);
ruleView.setSelected(true);
}
ruleView.setOnClickListener(v -> {
clearSelected(layoutRules);
ruleView.setSelected(true);
button.setSelected(true);
changeSkuWithRule();
});
}
layoutRules.addView(ruleView, layoutParams);
});
layoutSpecs.addView(specView);
// layoutRules.setOnCheckedChangeListener((group, checkedId) -> changeSkuWithRule());
});
changeSkuWithRule();
}
private void clearSelected(ViewGroup group) {
if (group.getChildCount() <= 0) {
......@@ -585,7 +590,7 @@ public class GoodsDetailDialog extends Dialog {
});
}
private void processSpecRules() {
private void processSelloutRules() {
if (mGoods == null || CollectionUtils.isEmpty(mGoods.getSpecs()) || CollectionUtils.isEmpty(mGoods.getSkus())) {
showSelloutViews();
return;
......@@ -625,6 +630,83 @@ public class GoodsDetailDialog extends Dialog {
});
}
private void processDeleteRules() {
if (mGoods == null || CollectionUtils.isEmpty(mGoods.getSpecs()) || CollectionUtils.isEmpty(mGoods.getSkus())) {
showSelloutViews();
return;
}
mGoods.getSpecs().forEach(spec -> {
if (CollectionUtils.isEmpty(spec.getRules())) {
return;
}
spec.getRules().forEach(rule -> rule.setIsDelete(Base.TRUE));
});
// 将商品所有SKU列表按删除和未删除分组
Map<Integer, List<Sku>> map = mGoods.getSkus().stream().collect(Collectors.groupingBy(Sku::getIsDelete));
if (CollectionUtils.isEmpty(map)) {
return;
}
// 删除SKU列表
List<Sku> deletes = map.get(Base.DELETE);
// 未删除SKU列表
List<Sku> uses = map.get(0);
if (CollectionUtils.isEmpty(uses)) {
showSelloutViews();
return;
}
// 售罄SKU列表为空不需要处理
if (CollectionUtils.isEmpty(deletes)) {
return;
}
deletes.forEach(sku -> {
if (CollectionUtils.isEmpty(sku.getRules())) {
return;
}
sku.getRules().forEach(skuRule -> {
// 检查未售罄的sku里是否有此规格选项,如果有则此选项可选
Integer ruleState = checkRuleDelete(skuRule, uses);
setRuleDelete(skuRule, ruleState);
});
});
}
private void setRuleDelete(SkuRule skuRule, Integer ruleDelete) {
if (mGoods == null || CollectionUtils.isEmpty(mGoods.getSpecs())) {
return;
}
mGoods.getSpecs().forEach(spec -> {
// 选项为空的规格不处理
if (CollectionUtils.isEmpty(spec.getRules())) {
return;
}
spec.getRules().forEach(rule -> {
// 设置匹配的选项
if (rule.getRuleId().equals(skuRule.getRuleId())) {
rule.setIsDelete(ruleDelete);
}
});
});
}
/** 检查未售罄的sku里是否有此规格选项,如果有则此选项可选 */
private Integer checkRuleDelete(SkuRule skuRule, List<Sku> uses) {
if (CollectionUtils.isEmpty(uses)) {
return Base.DELETE;
}
for (Sku sku : uses) {
if (CollectionUtils.isEmpty(sku.getRules())) {
continue;
}
for (SkuRule rule : sku.getRules()) {
if (rule.getRuleId().equals(skuRule.getRuleId())) {
return Base.TRUE;
}
}
}
return Base.DELETE;
}
/** 检查未售罄的sku里是否有此规格选项,如果有则此选项可选 */
private Integer checkRuleState(SkuRule skuRule, List<Sku> sellIns) {
if (CollectionUtils.isEmpty(sellIns)) {
......
......@@ -145,7 +145,7 @@ public class LauncherActivity extends Activity {
private void initSettingsQueue() {
View imgLogo = findViewById(R.id.img_logo);
new SettingsQueue(this, imgLogo, txtCategory);
new SettingsQueue(this, imgLogo);
}
private void getMachineCode() {
......@@ -417,6 +417,7 @@ public class LauncherActivity extends Activity {
Object obj = msg.obj;
if (msg.what == 0 || obj == null) {
Utils.showToast(LauncherActivity.this, R.string.error_load_main_data);
postDelayed(() -> loadData(), 1000 * 5);
return;
}
MainData mainData = JsonUtils.getMapper().readValue(obj.toString(), MainData.class);
......
package com.ihaoin.hooloo.device.view;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.util.StringUtils;
import com.ihaoin.hooloo.device.util.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PwdDialog extends Dialog {
private EditText p0;
private EditText p1;
private EditText p2;
private EditText p3;
private EditText p4;
private EditText p5;
private TextView txtTips;
private View butnClose;
private List<EditText> views = new ArrayList<>();
public PwdDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog_pwd);
setCancelable(false);
setCanceledOnTouchOutside(false);
initViews();
}
private void initViews() {
p0 = findViewById(R.id.p0);
p1 = findViewById(R.id.p1);
p2 = findViewById(R.id.p2);
p3 = findViewById(R.id.p3);
p4 = findViewById(R.id.p4);
p5 = findViewById(R.id.p5);
txtTips = findViewById(R.id.txt_tips);
// 下一个
p0.setTag(R.string.key_next, p1);
p1.setTag(R.string.key_next, p2);
p2.setTag(R.string.key_next, p3);
p3.setTag(R.string.key_next, p4);
p4.setTag(R.string.key_next, p5);
// 上一个
p1.setTag(R.string.key_prev, p0);
p2.setTag(R.string.key_prev, p1);
p3.setTag(R.string.key_prev, p2);
p4.setTag(R.string.key_prev, p3);
p5.setTag(R.string.key_prev, p4);
butnClose = findViewById(R.id.butn_close);
butnClose.setOnClickListener(x -> dismiss());
views = Arrays.asList(p0, p1, p2, p3, p4, p5);
clear();
setOnDismissListener(dismissListener);
views.forEach(v -> {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
v.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// if (!StringUtils.isEmpty(v.getText().toString())) {
// moveNext(v);
// }
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (StringUtils.isEmpty(v.getText().toString())) {
movePrev(v);
} else {
moveNext(v);
}
matchPwd();
}
});
v.setOnFocusChangeListener((view, b) -> {
if (b) {
v.setSelection(v.getText().length());
}
});
v.setOnKeyListener((view, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (StringUtils.isEmpty(v.getText().toString())) {
movePrev(v);
}
}
return false;
});
});
}
private void matchPwd() {
String pwd = "";
for (int i = 0; i < views.size(); i++) {
String s = views.get(i).getText().toString();
if (StringUtils.isEmpty(s)) {
return;
}
pwd += s;
}
if (pwd.equals("131105")) {
Utils.openAppSettings(getContext());
clear();
dismiss();
} else {
clear();
txtTips.setVisibility(View.VISIBLE);
// Utils.showToast(getContext(), "密码错误");
}
}
private void movePrev(TextView v) {
EditText t = (EditText) v.getTag(R.string.key_prev);
if (t == null) {
return;
}
t.requestFocus();
}
private void moveNext(TextView v) {
EditText t = (EditText) v.getTag(R.string.key_next);
if (t == null) {
return;
}
t.requestFocus();
}
// private void setSeleced(int index) {
// for (int i = 0; i < views.size(); i++) {
// if (i == index) {
// views.get(i).setSelected(true);
// } else {
// views.get(i).setSelected(false);
// }
// }
// }
private void clear() {
views.forEach(v -> v.setText(""));
views.get(0).requestFocus();
}
OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
views.forEach(v -> {
v.setTag(R.string.key_prev, null);
v.setTag(R.string.key_next, null);
});
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="0.5px"
android:color="@color/textPrimary" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<item android:drawable="@drawable/bg_button" android:state_focused="true" />
<item android:drawable="@drawable/bg_pwd" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="@dimen/dialog_clear_width"
android:layout_height="wrap_content"
android:background="@drawable/bg_dialog_clear">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="45px"
android:paddingBottom="45px">
<TextView
android:id="@+id/txt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/pwd_tips"
android:textColor="@color/textPrimary"
android:textSize="36px" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45px"
android:gravity="center_horizontal"
android:orientation="horizontal">
<EditText
android:id="@+id/p0"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
<EditText
android:id="@+id/p1"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:layout_marginStart="20px"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
<EditText
android:id="@+id/p2"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:layout_marginStart="20px"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
<EditText
android:id="@+id/p3"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:layout_marginStart="20px"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
<EditText
android:id="@+id/p4"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:layout_marginStart="20px"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
<EditText
android:id="@+id/p5"
android:layout_width="@dimen/pwd_input_size"
android:layout_height="@dimen/pwd_input_size"
android:layout_marginStart="20px"
android:background="@drawable/sel_pwd"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="numberPassword"
android:lines="1"
android:maxLength="1"
android:text="1"
android:textSize="32px" />
</LinearLayout>
<TextView
android:id="@+id/txt_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:gravity="center"
android:text="@string/pwd_error"
android:textColor="@android:color/holo_red_dark"
android:textSize="28px"
android:visibility="invisible" />
</LinearLayout>
<FrameLayout
android:id="@+id/butn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="@dimen/dialog_close_margin"
android:layout_marginTop="@dimen/dialog_close_margin"
android:layout_marginEnd="@dimen/dialog_close_margin"
android:layout_marginBottom="@dimen/dialog_close_margin"
android:padding="20px">
<ImageView
android:layout_width="@dimen/ic_dialog_close"
android:layout_height="@dimen/ic_dialog_close"
android:src="@mipmap/chazi" />
</FrameLayout>
</RelativeLayout>
\ No newline at end of file
......@@ -99,6 +99,7 @@
<dimen name="dialog_close_margin">35px</dimen>
<dimen name="clear_butn_height">100px</dimen>
<dimen name="clear_butn_text">40px</dimen>
<dimen name="pwd_input_size">50px</dimen>
<dimen name="trolley_width">850px</dimen>
<dimen name="trolley_item_height">210px</dimen>
......
......@@ -94,11 +94,12 @@
<dimen name="dialog_detail_width">600dp</dimen>
<dimen name="dialog_tip_width">600dp</dimen>
<dimen name="dialog_clear_width">715px</dimen>
<dimen name="dialog_clear_width">350dp</dimen>
<dimen name="dialog_clear_height">385px</dimen>
<dimen name="dialog_close_margin">35px</dimen>
<dimen name="clear_butn_height">100px</dimen>
<dimen name="clear_butn_text">40px</dimen>
<dimen name="pwd_input_size">40dp</dimen>
<dimen name="trolley_width">400dp</dimen>
<dimen name="trolley_item_height">100dp</dimen>
......
......@@ -15,6 +15,8 @@
<string name="error_check_sku_wrong">商品状态错误</string>
<string name="error_check_sku">商品已售罄</string>
<string name="clear_tips">是否清空购物车(%ss)</string>
<string name="pwd_tips">输入系统密码</string>
<string name="pwd_error">密码错误!</string>
<string name="txt_outside_temp">室外温度: %s°</string>
<string name="txt_inside_temp">店内温度: %s°</string>
<string name="txt_fridge_temp">冰箱温度: %s°</string>
......@@ -22,4 +24,6 @@
<string name="txt_timer_clear">咖啡机清洗时间: %s</string>
<string name="txt_timer_material">原料更换时间: %s</string>
<string name="txt_timer_usual">日常保洁时间: %s</string>
<string name="key_prev">prev</string>
<string name="key_next">next</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment