|
|
package com.xypower.mpremote;
|
|
|
|
|
|
import androidx.activity.result.ActivityResult;
|
|
|
import androidx.activity.result.ActivityResultCallback;
|
|
|
import androidx.activity.result.ActivityResultLauncher;
|
|
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
import android.Manifest;
|
|
|
import android.app.AlertDialog;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
import android.content.pm.PackageManager;
|
|
|
import android.graphics.Color;
|
|
|
import android.net.DhcpInfo;
|
|
|
import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.provider.Settings;
|
|
|
import android.text.TextUtils;
|
|
|
import android.text.format.Formatter;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
|
|
|
import com.xypower.mpremote.databinding.ActivityMainBinding;
|
|
|
import com.xypower.mpremote.utils.AppUtils;
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
|
// Used to load the 'mpremote' library on application startup.
|
|
|
private static final String TAG = "ADB";
|
|
|
|
|
|
private static final String DEFAULT_PRE_SHARED_KEY = SettingsActivity.DEFAULT_PRE_SHARED_KEY;
|
|
|
|
|
|
private static final int REQUEST_CODE_PERMISSIONS = 1025;
|
|
|
|
|
|
private static final String WIFI_IP_PREFIX = "192.168.";
|
|
|
|
|
|
private ActivityMainBinding binding;
|
|
|
private WifiManager wifiManager;
|
|
|
private ActivityResultLauncher<Intent> wifiLauncher;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
binding = ActivityMainBinding.inflate(getLayoutInflater());
|
|
|
setContentView(binding.getRoot());
|
|
|
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);//获取wifi管理器
|
|
|
initView();//初始化页面显示
|
|
|
initActivityResult();//初始化页面回调
|
|
|
initEvent();//初始化事件
|
|
|
}
|
|
|
|
|
|
private void initEvent() {
|
|
|
AdbManager.initManager(this);//初始化Adb
|
|
|
if (applyForPermissions()) {
|
|
|
getConnectWiFi();//获取已连接wifi
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void initView() {
|
|
|
// 设置完全透明状态栏
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
getWindow().getDecorView().setSystemUiVisibility(
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
);
|
|
|
getWindow().setStatusBarColor(Color.TRANSPARENT);
|
|
|
}
|
|
|
String versionName = AppUtils.getAppNameWithVersion(this);
|
|
|
binding.toolbar.title.setText(versionName);
|
|
|
binding.toolbar.back.setVisibility(View.INVISIBLE);
|
|
|
binding.selectWifi.setOnClickListener(this);
|
|
|
binding.control.setOnClickListener(this);
|
|
|
}
|
|
|
|
|
|
private void initActivityResult() {
|
|
|
wifiLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
|
|
|
@Override
|
|
|
public void onActivityResult(ActivityResult result) {
|
|
|
getConnectWiFi();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
try {
|
|
|
ArrayList<String> requestList = new ArrayList<>();//允许询问列表
|
|
|
ArrayList<String> banList = new ArrayList<>();//禁止列表
|
|
|
for (int i = 0; i < permissions.length; i++) {
|
|
|
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
|
|
|
Log.i(TAG, "【" + permissions[i] + "】权限授权成功");
|
|
|
} else {
|
|
|
//判断是否允许重新申请该权限
|
|
|
boolean nRet = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i]);
|
|
|
Log.i(TAG, "shouldShowRequestPermissionRationale nRet=" + nRet);
|
|
|
if (nRet) {//允许重新申请
|
|
|
requestList.add(permissions[i]);
|
|
|
} else {//禁止申请
|
|
|
banList.add(permissions[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//优先对禁止列表进行判断
|
|
|
if (banList.size() > 0) {
|
|
|
//告知该权限作用,要求手动授予权限
|
|
|
showFinishedDialog();
|
|
|
} else if (requestList.size() > 0) {
|
|
|
//告知权限的作用,并重新申请
|
|
|
showTipDialog(requestList);
|
|
|
} else {
|
|
|
// showToast("权限授权成功");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
// showToast("权限申请回调中发生异常");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void showFinishedDialog() {
|
|
|
AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
|
|
|
.setTitle("警告")
|
|
|
.setMessage("请前往设置中打开相关权限,否则功能无法正常运行!")
|
|
|
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
// 一般情况下如果用户不授权的话,功能是无法运行的,做退出处理
|
|
|
finish();
|
|
|
}
|
|
|
})
|
|
|
.create();
|
|
|
dialog.show();
|
|
|
}
|
|
|
|
|
|
public void showTipDialog(ArrayList<String> pmList) {
|
|
|
AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
|
|
|
.setTitle("提示")
|
|
|
.setMessage("【" + pmList.toString() + "】权限为应用必要权限,请授权")
|
|
|
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
String[] sList = pmList.toArray(new String[0]);
|
|
|
//重新申请该权限
|
|
|
ActivityCompat.requestPermissions(MainActivity.this, sList, REQUEST_CODE_PERMISSIONS);
|
|
|
}
|
|
|
})
|
|
|
.create();
|
|
|
dialog.show();
|
|
|
}
|
|
|
|
|
|
//页面按钮的点击事件
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
switch (v.getId()) {
|
|
|
case R.id.selectWifi:
|
|
|
openWifiActivity();
|
|
|
break;
|
|
|
case R.id.control:
|
|
|
openDeviceActivity();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取已连接WIFI
|
|
|
private void getConnectWiFi() {
|
|
|
if (wifiManager != null) {
|
|
|
if (!wifiManager.isWifiEnabled()) {
|
|
|
wifiManager.setWifiEnabled(true);
|
|
|
}
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
|
if (wifiInfo != null) {
|
|
|
String ssid = wifiInfo.getSSID();
|
|
|
if (!TextUtils.isEmpty(ssid)) {
|
|
|
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
|
|
|
ssid = ssid.substring(1, ssid.length() - 1);
|
|
|
}
|
|
|
}
|
|
|
binding.selectWifi.setText("已连接WIFI:" + ssid);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
public boolean applyForPermissions() {
|
|
|
|
|
|
String[] PM_MULTIPLE = {
|
|
|
Manifest.permission.ACCESS_COARSE_LOCATION,
|
|
|
Manifest.permission.READ_PHONE_STATE,
|
|
|
Manifest.permission.CHANGE_NETWORK_STATE,
|
|
|
Manifest.permission.CHANGE_WIFI_STATE,
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION,
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
|
|
};
|
|
|
|
|
|
try {
|
|
|
ArrayList<String> pmList = new ArrayList<>();
|
|
|
//获取当前未授权的权限列表
|
|
|
for (String permission : PM_MULTIPLE) {
|
|
|
int nRet = ContextCompat.checkSelfPermission(this, permission);
|
|
|
if (nRet != PackageManager.PERMISSION_GRANTED) {
|
|
|
pmList.add(permission);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (pmList.size() > 0) {
|
|
|
// Log.i(TAG,"进行权限申请...");
|
|
|
String[] sList = pmList.toArray(new String[0]);
|
|
|
ActivityCompat.requestPermissions(this, sList, REQUEST_CODE_PERMISSIONS);
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
//打开设备控制页面
|
|
|
protected void openDeviceActivity() {
|
|
|
String ipAddressByWifi = null;
|
|
|
if (wifiManager != null) {
|
|
|
if (!wifiManager.isWifiEnabled()) {
|
|
|
wifiManager.setWifiEnabled(true);
|
|
|
}
|
|
|
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
|
|
|
ipAddressByWifi = Formatter.formatIpAddress(dhcpInfo.ipAddress);
|
|
|
if (ipAddressByWifi.contains(WIFI_IP_PREFIX)) {
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
|
String localIP = Formatter.formatIpAddress(wifiInfo.getIpAddress());
|
|
|
String deviceIP = Formatter.formatIpAddress(dhcpInfo.gateway);
|
|
|
Intent intent = new Intent(MainActivity.this, DeviceActivity.class);
|
|
|
intent.putExtra("deviceIp", deviceIP);
|
|
|
intent.putExtra("localIp", localIP);
|
|
|
startActivity(intent);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//打开WIFI连接页面
|
|
|
private void openWifiActivity() {
|
|
|
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
|
|
|
wifiLauncher.launch(intent);
|
|
|
}
|
|
|
|
|
|
} |