diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 81d69e0..df6e876 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ + diff --git a/app/src/main/java/com/uiuios/aios/activity/ControlActivity.java b/app/src/main/java/com/uiuios/aios/activity/ControlActivity.java index 5265a02..39f2351 100644 --- a/app/src/main/java/com/uiuios/aios/activity/ControlActivity.java +++ b/app/src/main/java/com/uiuios/aios/activity/ControlActivity.java @@ -15,8 +15,11 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.BatteryManager; +import android.os.Build; import android.os.Bundle; import android.provider.Settings; +import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.SeekBar; @@ -32,8 +35,14 @@ import com.uiuios.aios.R; import com.uiuios.aios.manager.AmapManager; import com.uiuios.aios.utils.BrightnessUtils; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; import butterknife.BindView; import butterknife.ButterKnife; @@ -104,6 +113,8 @@ public class ControlActivity extends AppCompatActivity { registerReceivers(); getBluetooth(); registerBluetoothReceiver(); + getBattery(); + registerBatteryReceiver(); getFlashlight(); getFontSize(); getLocation(); @@ -119,7 +130,7 @@ public class ControlActivity extends AppCompatActivity { } }); if (isWifiEnabled()) { - tv_wifi_ssid.setText(getSSID()); + tv_wifi_ssid.setText(getConnectWifiSsid()); cl_wifi.setBackground(getDrawable(R.drawable.control_background_item)); } else { tv_wifi_ssid.setText("未连接"); @@ -183,7 +194,7 @@ public class ControlActivity extends AppCompatActivity { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //获取当前wifi名称 String newSSID = wifiInfo.getSSID(); - tv_wifi_ssid.setText(newSSID); + tv_wifi_ssid.setText(newSSID.replaceAll("\"", "")); cl_wifi.setBackground(getDrawable(R.drawable.control_background_item)); } break; @@ -220,7 +231,37 @@ public class ControlActivity extends AppCompatActivity { return ""; } + private String getConnectWifiSsid() { + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); +// //去掉带引号的字符串方法一 +// String wifiInfo1 = wifiInfo.getSSID(); +// if (wifiInfo1.contains("\"")) { +// wifiInfo1 = wifiInfo1.substring(1, wifiInfo1.length() - 1); +// } +//去掉带引号的字符串方法二 + String wifiSSID = wifiInfo.getSSID(); + String wifiInfo1 = wifiSSID.replaceAll("\"", ""); + return wifiInfo1; + } + + private BluetoothAdapter bluetoothAdapter; + private void getBluetooth() { + bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (bluetoothAdapter.isEnabled()) { + if (isConnected()) { + cl_bt.setBackground(getDrawable(R.drawable.control_background_item)); + tv_bt_ssid.setText(getBluetoothDeviceName()); + } else { + cl_bt.setBackground(getDrawable(R.drawable.control_background_item_dis)); + tv_bt_ssid.setText("未连接"); + } + } else { + cl_bt.setBackground(getDrawable(R.drawable.control_background_item_dis)); + tv_bt_ssid.setText("已关闭"); + } + getConnectedDevicesV1(); cl_bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -229,6 +270,109 @@ public class ControlActivity extends AppCompatActivity { }); } + public boolean isConnected() { + Set bondedDevices = bluetoothAdapter.getBondedDevices(); + List deviceList = bondedDevices.stream().filter(new Predicate() { + @Override + public boolean test(BluetoothDevice bluetoothDevice) { + return bluetoothDevice.isConnected(); + } + }).collect(Collectors.toList()); + return deviceList.size() > 0; + } + + public String getBluetoothDeviceName() { + Set bondedDevices = bluetoothAdapter.getBondedDevices(); + List deviceList = bondedDevices.stream().filter(new Predicate() { + @Override + public boolean test(BluetoothDevice bluetoothDevice) { + return bluetoothDevice.isConnected(); + } + }).collect(Collectors.toList()); + if (deviceList.size() == 0) { + return "未连接"; + } else { + return deviceList.get(0).getName(); + } + } + + //TODO 根据mac地址判断是否已连接(这里参数可以直接用BluetoothDevice对象) +//但这么写其实更通用。 + public boolean isConnected(String macAddress) { + if (!BluetoothAdapter.checkBluetoothAddress(macAddress)) { + return false; + } + final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress); + + Method isConnectedMethod = null; + boolean isConnected; + try { + isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null); + isConnectedMethod.setAccessible(true); + isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null); + } catch (NoSuchMethodException e) { + isConnected = false; + } catch (IllegalAccessException e) { + isConnected = false; + } catch (InvocationTargetException e) { + isConnected = false; + } + return isConnected; + } + + /** + * 获取系统中已连接的蓝牙设备 + * + * @return + */ + public Set getConnectedDevicesV1() { + Class bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象 + Set deviceSet = new HashSet<>(); + //是否存在连接的蓝牙设备 + try { + Method method = bluetoothAdapterClass.getDeclaredMethod("getMostRecentlyConnectedDevices", (Class[]) null); + //打开权限 + method.setAccessible(true); + List list = (List) method.invoke(BluetoothAdapter.getDefaultAdapter(), (Object[]) null); + Log.e("zbh", "最近连接过的设备:"); + for (BluetoothDevice dev : list + ) { + String Type = ""; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + switch (dev.getType()) { + case BluetoothDevice.DEVICE_TYPE_CLASSIC: + Type = "经典"; + break; + case BluetoothDevice.DEVICE_TYPE_LE: + Type = "BLE"; + break; + case BluetoothDevice.DEVICE_TYPE_DUAL: + Type = "双模"; + break; + default: + Type = "未知"; + break; + } + } + String connect = "设备未连接"; + if (isConnected(dev.getAddress())) { + deviceSet.add(dev); + connect = "设备已连接"; + } + Log.e("zbh", connect + ", address = " + dev.getAddress() + "(" + Type + "), name --> " + dev.getName()); + + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return deviceSet; + } + private BluetoothMonitorReceiver bleListenerReceiver; private void registerBluetoothReceiver() { @@ -247,36 +391,37 @@ public class ControlActivity extends AppCompatActivity { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action != null) { + Log.e("BluetoothMonitorReceiver", "onReceive: " + action); + if (!TextUtils.isEmpty(action)) { 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("蓝牙正在打开"); + tv_bt_ssid.setText("正在打开"); cl_bt.setBackground(getDrawable(R.drawable.control_background_item)); break; case BluetoothAdapter.STATE_ON: - tv_bt_ssid.setText("蓝牙已经打开"); + tv_bt_ssid.setText("已打开"); cl_bt.setBackground(getDrawable(R.drawable.control_background_item)); break; case BluetoothAdapter.STATE_TURNING_OFF: - tv_bt_ssid.setText("蓝牙正在关闭"); + tv_bt_ssid.setText("正在关闭"); cl_bt.setBackground(getDrawable(R.drawable.control_background_item)); break; case BluetoothAdapter.STATE_OFF: - tv_bt_ssid.setText("关"); + 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("蓝牙设备已连接"); + tv_bt_ssid.setText(getBluetoothDeviceName()); cl_bt.setBackground(getDrawable(R.drawable.control_background_item)); break; case BluetoothDevice.ACTION_ACL_DISCONNECTED: - tv_bt_ssid.setText("蓝牙设备已断开"); + tv_bt_ssid.setText("未连接"); cl_bt.setBackground(getDrawable(R.drawable.control_background_item_dis)); break; default: @@ -286,6 +431,90 @@ public class ControlActivity extends AppCompatActivity { } } + private void getBattery() { + tv_electricity.setText(getBatteryCapacity() + "%"); + if (isBatteryCharging()) { + cl_battery.setBackground(getDrawable(R.drawable.control_background_item)); + } else { + cl_battery.setBackground(getDrawable(R.drawable.control_background_item_dis)); + } + } + + /** + * 主动获取当前电池是否在充电 , 即数据线是否插在手机上 + * + * @return + */ + public boolean isBatteryCharging() { + boolean isBatteryCharging = false; + // 主动发送包含是否正在充电状态的广播 , 该广播会持续发送 + IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + // 注册广播接受者 + Intent intent = registerReceiver(null, intentFilter); + // 获取充电状态 + int batteryChargeState = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); + // 判定是否是 AC 交流电充电 + boolean isAc = batteryChargeState == BatteryManager.BATTERY_PLUGGED_AC; + // 判断是否是 USB 充电 + boolean isUsb = batteryChargeState == BatteryManager.BATTERY_PLUGGED_USB; + // 判断是否是 无线充电 + boolean isWireless = batteryChargeState == BatteryManager.BATTERY_PLUGGED_WIRELESS; + // 如何上述任意一种为 true , 说明当前正在充电 + isBatteryCharging = isAc || isUsb || isWireless; + return isBatteryCharging; + } + + + public int getBatteryCapacity() { + try { + BatteryManager batteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE); + return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); + } catch (Exception e) { + Log.e("getBattery", "getBattery" + e.getMessage()); + } + return 0; + } + + private BatteryReceiver mBatteryReceiver; + + private void registerBatteryReceiver() { + if (mBatteryReceiver == null) { + mBatteryReceiver = new BatteryReceiver(); + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_POWER_CONNECTED); + filter.addAction(Intent.ACTION_POWER_DISCONNECTED); + filter.addAction(Intent.ACTION_BATTERY_CHANGED); + filter.addAction(Intent.ACTION_BATTERY_LEVEL_CHANGED); + filter.addAction(Intent.ACTION_BATTERY_LOW); + filter.addAction(Intent.ACTION_BATTERY_OKAY); + registerReceiver(mBatteryReceiver, filter); + } + } + + public class BatteryReceiver extends BroadcastReceiver { + public static final String TAG = "BatteryReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + // 获取广播事件 + String action = intent.getAction(); + if (TextUtils.isEmpty(action)) return; + Log.e(TAG, "onReceive: " + action); + switch (action) { + case Intent.ACTION_POWER_CONNECTED: + cl_battery.setBackground(getDrawable(R.drawable.control_background_item)); + break; + case Intent.ACTION_POWER_DISCONNECTED: + cl_battery.setBackground(getDrawable(R.drawable.control_background_item_dis)); + break; + case Intent.ACTION_BATTERY_LEVEL_CHANGED: + tv_electricity.setText(getBatteryCapacity() + "%"); + break; + default: + } + } + } + private void getFlashlight() { if (isFlashlightEnabled()) { tv_flashlight_switch.setText("开"); @@ -407,7 +636,6 @@ public class ControlActivity extends AppCompatActivity { } private void getBrightness() { - seekbar_brightness.setMin(1); seekbar_brightness.setMax(255); //亮度 int brightness = Settings.System.getInt(crv, Settings.System.SCREEN_BRIGHTNESS, 1); @@ -421,9 +649,12 @@ public class ControlActivity extends AppCompatActivity { seekbar_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { + Log.e(TAG, "onProgressChanged: i = " + i); Settings.System.putInt(crv, Settings.System.SCREEN_BRIGHTNESS, i); int gamma = BrightnessUtils.convertLinearToGamma(i, 1, 255); + Log.e(TAG, "onProgressChanged: gamma = " + gamma); long percentage = Math.round((((double) gamma / 65535) * 100f)); + Log.e(TAG, "onProgressChanged: percentage = " + percentage); tv_brightness.setText(percentage + "%"); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 53de10b..f9ee09d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,5 @@ - 关怀系统OS + AIOS系统 Hello blank fragment