DevScanQRActivity.java 源代码
package activity.addCamera;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import base.HiActivity;
import com.hichip.campro.R;
import com.hichip.tools.HiLitosSDK;
import com.hichip.tools.Packet;
import com.xiaomi.mipush.sdk.Constants;
import common.Constant;
import common.HiDataValue;
import common.TitleView;
import java.util.Random;
import liteos.Zxing.CreateQRCode_Utils;
import liteos.addCamera.OSAddCameraActivity;
import liteos.addCamera.OSWaitQrcodeAddConfigActivity;
import main.MainActivity;
import org.apache.poi.ss.formula.ptg.Ptg;
import utils.HiLogcatUtil;
import utils.HiTools;
import utils.MyToast;
import utils.WifiUtils;
public class DevScanQRActivity extends HiActivity {
private static final int MY_PERMISSION_REQUEST_CODE = 10009;
private Animatable animatable;
TextView but_search_device;
private float defaultLight;
HiLitosSDK hiLitosSDK;
ImageView iv_qr;
private WindowManager.LayoutParams lp;
CheckBox mCheckbox;
private String mCurrentPhoneWiFi;
ImageView mScanImageTip;
private String mUid = "";
TitleView titleView;
TextView tv_hint;
@Override
protected int setLayoutId() {
return R.layout.activity_dev_scan_qr;
}
@Override
protected void init(Bundle bundle) {
String stringExtra = getIntent().getStringExtra("mCurrentPhoneWiFi");
this.mCurrentPhoneWiFi = stringExtra;
if (TextUtils.isEmpty(stringExtra)) {
this.mCurrentPhoneWiFi = WifiUtils.getCurrentWifiSSID(this, true);
}
initTitle();
initLight();
initData();
setListener();
}
private void setListener() {
this.but_search_device.setOnClickListener(new View.OnClickListener() {
@Override
public final void onClick(View view) {
DevScanQRActivity.this.lambda$setListener$0$DevScanQRActivity(view);
}
});
this.mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
DevScanQRActivity.this.but_search_device.setEnabled(z);
}
});
Animatable animatable = (Animatable) this.mScanImageTip.getDrawable();
this.animatable = animatable;
animatable.start();
}
public void lambda$setListener$0$DevScanQRActivity(View view) {
if (HiTools.isWifiConnected(this) || HiDataValue.mHi_wifiConnect_Q.length() > 0) {
Intent intent = new Intent(this, (Class<?>) OSWaitQrcodeAddConfigActivity.class);
intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
intent.putExtra(HiDataValue.EXTRAS_KEY_UID, this.mUid);
startActivityForResult(intent, 1);
return;
}
MyToast.showToast(this, getString(R.string.toast_connect_wifi));
}
private void initData() {
String stringExtra = getIntent().getStringExtra("ssid");
String stringExtra2 = getIntent().getStringExtra("psw");
String iPAddress = HiTools.getIPAddress(this);
this.mUid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
int mPort = getMPort();
if (mPort == 0) {
mPort = 13567;
}
this.tv_hint.setText(getString(R.string.dev_scan_hint));
StringBuilder sb = new StringBuilder();
byte[] bytes = stringExtra.getBytes();
byte[] bytes2 = stringExtra2.getBytes();
int length = bytes.length;
int length2 = bytes2.length;
byte[] bArr = new byte[5];
System.arraycopy(Packet.shortToByteArray_Big((short) 24443), 0, bArr, 0, 2);
bArr[2] = (byte) length;
bArr[3] = (byte) length2;
if (iPAddress != null) {
bArr[4] = (byte) iPAddress.length();
}
for (int i = 0; i < length2; i++) {
if (bytes2[i] <= 63) {
bytes2[i] = (byte) (bytes2[i] + Ptg.CLASS_ARRAY);
} else if (bytes2[i] > 64) {
bytes2[i] = (byte) (bytes2[i] - Ptg.CLASS_ARRAY);
}
}
sb.append(new String(bArr));
sb.append(stringExtra);
sb.append(new String(bytes2));
sb.append(iPAddress);
sb.append(mPort);
Point point = new Point();
getWindowManager().getDefaultDisplay().getRealSize(point);
int dip2px = point.x - HiTools.dip2px(this, 30.0f);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
float f = dip2px;
int dip2px2 = HiTools.dip2px(this, f);
int dip2px3 = HiTools.dip2px(this, f);
Log.e("initData", point.x + Constants.COLON_SEPARATOR + dip2px + Constants.COLON_SEPARATOR + dip2px2 + Constants.COLON_SEPARATOR + dip2px3);
Log.e("initData", displayMetrics.widthPixels + "::" + (displayMetrics.widthPixels - HiTools.dip2px(this, 30.0f)) + Constants.COLON_SEPARATOR + displayMetrics.heightPixels + Constants.COLON_SEPARATOR + dip2px + Constants.COLON_SEPARATOR + dip2px2 + Constants.COLON_SEPARATOR + dip2px3);
Bitmap create2DCoderBitmap = CreateQRCode_Utils.create2DCoderBitmap(sb.toString().trim(), dip2px2, dip2px3);
Bitmap createBitmap = Bitmap.createBitmap(create2DCoderBitmap, create2DCoderBitmap.getWidth() / 10, create2DCoderBitmap.getHeight() / 10, (create2DCoderBitmap.getWidth() * 8) / 10, (create2DCoderBitmap.getHeight() * 8) / 10);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.iv_qr.getLayoutParams();
layoutParams.width = dip2px;
layoutParams.height = dip2px;
this.iv_qr.setLayoutParams(layoutParams);
this.iv_qr.setImageBitmap(createBitmap);
openServerEndReceiveMsg(mPort);
}
private int getMPort() {
return new Random().nextInt(20000) + 10000;
}
private void openServerEndReceiveMsg(int i) {
HiLitosSDK hiLitosSDK = new HiLitosSDK(new HiLitosSDK.ILitosResult() {
@Override
public final void onReceiveLitosResult(String str, int i2, int i3, int i4) {
DevScanQRActivity.this.lambda$openServerEndReceiveMsg$1$DevScanQRActivity(str, i2, i3, i4);
}
});
this.hiLitosSDK = hiLitosSDK;
hiLitosSDK.litoscreateServerSocket(i);
}
public void lambda$openServerEndReceiveMsg$1$DevScanQRActivity(String str, int i, int i2, int i3) {
((Vibrator) getSystemService("vibrator")).vibrate(400L);
HiLogcatUtil.i("tedu", "-recieve-:" + str + "-type-:" + i + "-state-:" + i2 + "-num-:" + i3);
if (TextUtils.isEmpty(str) || str.equals("0_0")) {
MyToast.showToast(this, getString(R.string.netword_abnormal));
return;
}
String[] split = str.split("_");
for (String str2 : split) {
HiLogcatUtil.i("tedu", "--str--:" + str2);
}
boolean checkIsUid = HiTools.checkIsUid(split[0]);
HiLogcatUtil.i("tedu", "--b--:" + checkIsUid);
if (checkIsUid) {
this.but_search_device.setEnabled(true);
Intent intent = new Intent(this, (Class<?>) OSAddCameraActivity.class);
intent.putExtra("mCurrentPhoneWiFi", this.mCurrentPhoneWiFi);
intent.putExtra("type", 1);
intent.putExtra(HiDataValue.EXTRAS_KEY_UID, split[0]);
intent.putExtra(Constant.MAC, split[1]);
startActivity(intent);
finish();
return;
}
if (TextUtils.isEmpty(split[0]) || HiTools.checkIsUid(split[0])) {
return;
}
MyToast.showToast(this, getString(R.string.input_uid_format_error));
}
private void initTitle() {
this.titleView.setButton(0);
this.titleView.setTitle(getString(R.string.dev_scan_qr_add));
this.titleView.setRightTxtBack(R.mipmap.x_bule);
this.titleView.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
@Override
public final void OnNavigationButtonClick(int i) {
DevScanQRActivity.this.lambda$initTitle$2$DevScanQRActivity(i);
}
});
}
public void lambda$initTitle$2$DevScanQRActivity(int i) {
if (i == 0) {
finish();
} else {
if (i != 2) {
return;
}
startActivity(new Intent(this, (Class<?>) MainActivity.class), true);
}
}
private void initLight() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
this.lp = attributes;
this.defaultLight = attributes.screenBrightness;
}
@Override
public void onResume() {
super.onResume();
this.lp.screenBrightness = 0.8000001f;
getWindow().setAttributes(this.lp);
Animatable animatable = this.animatable;
if (animatable == null || animatable.isRunning()) {
return;
}
this.animatable.start();
}
@Override
public void onPause() {
super.onPause();
this.lp.screenBrightness = this.defaultLight * 0.003921569f;
getWindow().setAttributes(this.lp);
Animatable animatable = this.animatable;
if (animatable != null) {
animatable.stop();
}
}
@Override
public void onDestroy() {
super.onDestroy();
HiLitosSDK hiLitosSDK = this.hiLitosSDK;
if (hiLitosSDK != null) {
hiLitosSDK.litosstopServerSocket();
}
this.hiLitosSDK = null;
}
@Override
public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
super.onRequestPermissionsResult(i, strArr, iArr);
boolean z = true;
HiLogcatUtil.e("" + i);
int length = strArr.length;
for (int i2 = 0; i2 < length; i2++) {
HiLogcatUtil.e(strArr.length + Constants.COLON_SEPARATOR + strArr[i2]);
}
for (int i3 : iArr) {
HiLogcatUtil.e(iArr.length + Constants.COLON_SEPARATOR + i3);
}
if (i == MY_PERMISSION_REQUEST_CODE) {
int length2 = iArr.length;
int i4 = 0;
while (true) {
if (i4 >= length2) {
break;
}
if (iArr[i4] != 0) {
z = false;
break;
}
i4++;
}
if (z) {
return;
}
HiTools.Hi_GoToSetting(strArr, this, this);
}
}
@Override
public void onActivityResult(int i, int i2, Intent intent) {
super.onActivityResult(i, i2, intent);
if (i2 == -1 && i == 1) {
finish();
}
}
}