version:1.2

fix:
update:更新app name,增加快捷控制
This commit is contained in:
2022-10-25 09:34:58 +08:00
parent 15e9854c48
commit 652135f45c
27 changed files with 1258 additions and 123 deletions

View File

@@ -0,0 +1,483 @@
package com.uiuios.aios.activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.uiuios.aios.R;
import com.uiuios.aios.manager.AmapManager;
import com.uiuios.aios.utils.BrightnessUtils;
import java.util.Arrays;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ControlActivity extends AppCompatActivity {
private static final String TAG = ControlActivity.class.getSimpleName();
@BindView(R.id.tv_wifi)
TextView tv_wifi;
@BindView(R.id.tv_wifi_ssid)
TextView tv_wifi_ssid;
@BindView(R.id.tv_bt)
TextView tv_bt;
@BindView(R.id.tv_bt_ssid)
TextView tv_bt_ssid;
@BindView(R.id.tv_font_size)
TextView tv_font_size;
@BindView(R.id.seekBar)
SeekBar seekBar;
@BindView(R.id.seekbar_brightness)
SeekBar seekbar_brightness;
@BindView(R.id.seekbar_sound)
SeekBar seekbar_sound;
@BindView(R.id.tv_sound)
TextView tv_sound;
@BindView(R.id.tv_battery)
TextView tv_battery;
@BindView(R.id.tv_electricity)
TextView tv_electricity;
@BindView(R.id.tv_flashlight)
TextView tv_flashlight;
@BindView(R.id.tv_flashlight_switch)
TextView tv_flashlight_switch;
@BindView(R.id.tv_brightness)
TextView tv_brightness;
@BindView(R.id.tv_location)
TextView tv_location;
@BindView(R.id.cl_wifi)
ConstraintLayout cl_wifi;
@BindView(R.id.cl_bt)
ConstraintLayout cl_bt;
@BindView(R.id.cl_battery)
ConstraintLayout cl_battery;
@BindView(R.id.cl_flashlight)
ConstraintLayout cl_flashlight;
private ContentResolver crv;
private static final String ACTION_FLASHLIGHT_CHANGED =
"com.android.settings.flashlight.action.FLASHLIGHT_CHANGED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
ButterKnife.bind(this);
crv = getContentResolver();
initView();
}
private void initView() {
getWifi();
registerReceivers();
getBluetooth();
registerBluetoothReceiver();
getFlashlight();
getFontSize();
getLocation();
getBrightness();
getSound();
}
private void getWifi() {
cl_wifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
});
if (isWifiEnabled()) {
tv_wifi_ssid.setText(getSSID());
cl_wifi.setBackground(getDrawable(R.drawable.control_background_item));
} else {
tv_wifi_ssid.setText("未连接");
cl_wifi.setBackground(getDrawable(R.drawable.control_background_item_dis));
}
}
public boolean isWifiEnabled() {
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiInfo.isConnected();
} else {
return false;
}
}
private WifiReceiver mWifiReceiver;
private void registerReceivers() {
registerWiFiReceiver();
}
private void registerWiFiReceiver() {
if (mWifiReceiver == null) {
mWifiReceiver = new WifiReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mWifiReceiver, filter);
}
public class WifiReceiver extends BroadcastReceiver {
private static final String TAG = "wifiReceiver";
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case WifiManager.RSSI_CHANGED_ACTION:
Log.e(TAG, "wifi信号强度变化");
break;
//wifi连接上与否
case WifiManager.NETWORK_STATE_CHANGED_ACTION:
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
Log.e(TAG, "wifi断开");
tv_wifi_ssid.setText("未连接");
cl_wifi.setBackground(getDrawable(R.drawable.control_background_item_dis));
} else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//获取当前wifi名称
String newSSID = wifiInfo.getSSID();
tv_wifi_ssid.setText(newSSID);
cl_wifi.setBackground(getDrawable(R.drawable.control_background_item));
}
break;
//wifi打开与否
case WifiManager.WIFI_STATE_CHANGED_ACTION:
int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
Log.e(TAG, "系统关闭wifi");
tv_wifi_ssid.setText("");
cl_wifi.setBackground(getDrawable(R.drawable.control_background_item_dis));
} else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
Log.e(TAG, "系统开启wifi");
}
break;
default:
}
}
}
/**
* 获取当前连接WIFI的SSID
*/
public String getSSID() {
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
if (wm != null) {
WifiInfo winfo = wm.getConnectionInfo();
if (winfo != null) {
String s = winfo.getSSID();
if (s.length() > 2 && s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
return s.substring(1, s.length() - 1);
}
}
}
return "";
}
private void getBluetooth() {
cl_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
}
});
}
private BluetoothMonitorReceiver bleListenerReceiver;
private void registerBluetoothReceiver() {
bleListenerReceiver = new BluetoothMonitorReceiver();
IntentFilter intentFilter = new IntentFilter();
// 监视蓝牙关闭和打开的状态
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
// 监视蓝牙设备与APP连接的状态
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
// 注册广播
registerReceiver(this.bleListenerReceiver, intentFilter);
}
public class BluetoothMonitorReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
switch (action) {
case BluetoothAdapter.ACTION_STATE_CHANGED:
int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
switch (blueState) {
case BluetoothAdapter.STATE_TURNING_ON:
tv_bt_ssid.setText("蓝牙正在打开");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item));
break;
case BluetoothAdapter.STATE_ON:
tv_bt_ssid.setText("蓝牙已经打开");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item));
break;
case BluetoothAdapter.STATE_TURNING_OFF:
tv_bt_ssid.setText("蓝牙正在关闭");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item));
break;
case BluetoothAdapter.STATE_OFF:
tv_bt_ssid.setText("");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item_dis));
break;
default:
}
break;
case BluetoothDevice.ACTION_ACL_CONNECTED:
tv_bt_ssid.setText("蓝牙设备已连接");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item));
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
tv_bt_ssid.setText("蓝牙设备已断开");
cl_bt.setBackground(getDrawable(R.drawable.control_background_item_dis));
break;
default:
}
}
}
}
private void getFlashlight() {
if (isFlashlightEnabled()) {
tv_flashlight_switch.setText("");
} else {
tv_flashlight_switch.setText("");
}
cl_flashlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isFlashlightEnabled()) {
Settings.Secure.putInt(crv, Settings.Secure.FLASHLIGHT_ENABLED, 0);
sendBroadcast(new Intent(ACTION_FLASHLIGHT_CHANGED));
tv_flashlight_switch.setText("");
} else {
Settings.Secure.putInt(crv, Settings.Secure.FLASHLIGHT_ENABLED, 1);
sendBroadcast(new Intent(ACTION_FLASHLIGHT_CHANGED));
tv_flashlight_switch.setText("");
}
}
});
}
private boolean isFlashlightAvailable() {
int defaultAvailability = 0;
try {
// check if there is a flash unit
if (getCameraId(this) != null) {
defaultAvailability = 1;
}
} catch (CameraAccessException e) {
Log.e(TAG, "Error getting camera id.", e);
}
return Settings.Secure.getInt(crv, Settings.Secure.FLASHLIGHT_AVAILABLE, defaultAvailability) == 1;
}
private static String getCameraId(Context context) throws CameraAccessException {
final CameraManager cameraManager = context.getSystemService(CameraManager.class);
final String[] ids = cameraManager.getCameraIdList();
for (String id : ids) {
CameraCharacteristics c = cameraManager.getCameraCharacteristics(id);
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (flashAvailable != null && flashAvailable
&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
return id;
}
}
return null;
}
private boolean isFlashlightEnabled() {
return Settings.Secure.getInt(crv, Settings.Secure.FLASHLIGHT_ENABLED, 0) == 1;
}
private void getFontSize() {
float fontScale = Settings.System.getFloat(crv, Settings.System.FONT_SCALE, 0.0f);
Log.e(TAG, "getHardware: fontScale = " + fontScale);
List<String> mEntries = Arrays.asList(getResources().getStringArray(R.array.entries_font_size));
List<String> strEntryValues = Arrays.asList(getResources().getStringArray(R.array.entryvalues_font_size));
String font_size;
int index = strEntryValues.indexOf(String.valueOf(fontScale));
if (index == -1) {
font_size = "默认";
} else {
font_size = mEntries.get(index);
}
tv_font_size.setText(font_size);
seekBar.setProgress(index);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Settings.System.putFloat(crv, Settings.System.FONT_SCALE, Float.parseFloat(strEntryValues.get(i)));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void getLocation() {
BDLocation bdLocation = AmapManager.getInstance().getNowMapLocation();
if (bdLocation != null) {
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation // GPS定位结果
|| bdLocation.getLocType() == BDLocation.TypeNetWorkLocation // 网络定位结果
|| bdLocation.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
tv_location.setText(bdLocation.getAddrStr());
} else {
tv_location.setText("未知");
}
} else {
tv_location.setText("定位失败");
}
LocationClient locationClient = AmapManager.getInstance().getLocationClient();
locationClient.stop();
locationClient.start();
locationClient.registerLocationListener(new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
switch (bdLocation.getLocType()) {
case BDLocation.TypeGpsLocation:// GPS定位结果
case BDLocation.TypeNetWorkLocation:// 网络定位结果
case BDLocation.TypeOffLineLocation:// 离线定位结果
tv_location.setText(bdLocation.getAddrStr());
break;
default:
tv_location.setText("定位失败");
break;
}
}
});
}
private void getBrightness() {
seekbar_brightness.setMin(1);
seekbar_brightness.setMax(255);
//亮度
int brightness = Settings.System.getInt(crv, Settings.System.SCREEN_BRIGHTNESS, 1);
seekbar_brightness.setProgress(brightness);
Log.e(TAG, "getHardware: brightness = " + brightness);
int gamma = BrightnessUtils.convertLinearToGamma(brightness, 1, 255);
Log.e(TAG, "getHardware: gamma = " + gamma);
long percentage = Math.round((((double) gamma / 65535) * 100f));
tv_brightness.setText(percentage + "%");
Log.e(TAG, "getHardware: percentage = " + percentage);
seekbar_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Settings.System.putInt(crv, Settings.System.SCREEN_BRIGHTNESS, i);
int gamma = BrightnessUtils.convertLinearToGamma(i, 1, 255);
long percentage = Math.round((((double) gamma / 65535) * 100f));
tv_brightness.setText(percentage + "%");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private AudioManager mAudioManager;
private void getSound() {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//最大音量
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
seekbar_sound.setMax(maxVolume);
Log.e(TAG, "getHardware: maxVolume = " + maxVolume);
//音量
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
seekbar_sound.setProgress(streamVolume);
Log.e(TAG, "getHardware: streamVolume = " + streamVolume);
int currentVolume = (int) (((double) streamVolume / (double) maxVolume) * 100f);
Log.e(TAG, "getHardware: currentVolume = " + currentVolume);
tv_sound.setText(currentVolume + "%");
seekbar_sound.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int volume = (int) (((double) i / (double) maxVolume) * 100f);
tv_sound.setText(volume + "%");
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mWifiReceiver != null) unregisterReceiver(mWifiReceiver);
}
}