384 lines
18 KiB
Java
384 lines
18 KiB
Java
package com.ckkj.griddle.device;
|
||
|
||
import android.Manifest;
|
||
import android.content.ActivityNotFoundException;
|
||
import android.content.Intent;
|
||
import android.content.pm.PackageManager;
|
||
import android.net.Uri;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.provider.Settings;
|
||
import android.view.View;
|
||
|
||
import androidx.activity.result.ActivityResultLauncher;
|
||
import androidx.activity.result.contract.ActivityResultContracts;
|
||
import androidx.annotation.NonNull;
|
||
import androidx.annotation.Nullable;
|
||
import androidx.core.content.ContextCompat;
|
||
import androidx.recyclerview.widget.RecyclerView;
|
||
|
||
import com.ckkj.griddle.R;
|
||
import com.ckkj.griddle.base.BaseFragment;
|
||
import com.ckkj.griddle.databinding.DeviceIndexFragmentBinding;
|
||
import com.ckkj.griddle.device.adapter.HomeDeviceAdapter;
|
||
import com.ckkj.griddle.device.control.DevicePreviewActivity;
|
||
import com.ckkj.griddle.device.entity.HomeDeviceEntity;
|
||
import com.ckkj.griddle.device.mvp.DeviceContract;
|
||
import com.ckkj.griddle.device.mvp.DeviceImpl;
|
||
import com.ckkj.griddle.device.mvp.DevicePresenter;
|
||
import com.ckkj.griddle.network.BaseBean;
|
||
import com.ckkj.griddle.utils.show.ToastUtil;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import pub.devrel.easypermissions.EasyPermissions;
|
||
|
||
/**
|
||
* 设备首页。
|
||
*/
|
||
public class DeviceFragment extends BaseFragment<DeviceIndexFragmentBinding, DevicePresenter>
|
||
implements DeviceContract.DeviceView {
|
||
|
||
private RecyclerView.Adapter<?> mDeviceAdapter;
|
||
private RecyclerView.Adapter<?> mObservedAdapter;
|
||
// 从系统权限请求发起到结果回调之间保持为 true,防止连续点击重复申请。
|
||
private boolean bluetoothPermissionRequestInFlight;
|
||
private static final int REQUEST_HOME_LOCATION = 10004;
|
||
// 定位授权与蓝牙授权分开记录,避免两种权限回调互相触发业务。
|
||
private boolean locationPermissionRequestInFlight;
|
||
private boolean waitingForLocationSettings;
|
||
private final ActivityResultLauncher<Intent> locationSettingsLauncher =
|
||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
|
||
if (!waitingForLocationSettings) return;
|
||
waitingForLocationSettings = false;
|
||
if (!canUpdateLocationDemo()) return;
|
||
// 设置返回后只检查授权状态并展示模拟城市,不读取真实位置。
|
||
if (!hasLocationPermission()) {
|
||
ToastUtil.showShort(requireContext(), getString(R.string.device_home_location_permission_required));
|
||
} else {
|
||
showLocationDemo();
|
||
}
|
||
});
|
||
private final RecyclerView.AdapterDataObserver mDeviceDataObserver =
|
||
new RecyclerView.AdapterDataObserver() {
|
||
@Override
|
||
public void onChanged() {
|
||
updateDeviceEmptyState();
|
||
}
|
||
|
||
@Override
|
||
public void onItemRangeInserted(int positionStart, int itemCount) {
|
||
updateDeviceEmptyState();
|
||
}
|
||
|
||
@Override
|
||
public void onItemRangeRemoved(int positionStart, int itemCount) {
|
||
updateDeviceEmptyState();
|
||
}
|
||
};
|
||
|
||
@Override
|
||
public DevicePresenter createPresenter() {
|
||
return new DevicePresenter(new DeviceImpl());
|
||
}
|
||
|
||
@Override
|
||
public void initData(Bundle state) {
|
||
if (mDeviceAdapter == null) {
|
||
initDemoDevices();
|
||
} else {
|
||
bindDeviceAdapter();
|
||
}
|
||
// 首页标题、右上角添加图标和空状态入口共用授权检查,避免某个入口绕过授权。
|
||
View.OnClickListener openDeviceList = v -> openBluetoothDeviceList();
|
||
viewBinding.tvHomeTitle.setOnClickListener(openDeviceList);
|
||
viewBinding.ivEmptyDevices.setOnClickListener(openDeviceList);
|
||
viewBinding.ivAddDevice.setOnClickListener(openDeviceList);
|
||
viewBinding.btnEmptyAddDevice.setOnClickListener(openDeviceList);
|
||
// 进入首页不申请位置权限;点击定位区域后才说明用途并申请。
|
||
viewBinding.llHomeLocation.setOnClickListener(v -> requestHomeLocationPermission());
|
||
}
|
||
|
||
/** 仅供首页展示:三台在线、一台离线;后续接口接入时替换此方法中的数据来源。 */
|
||
private void initDemoDevices() {
|
||
HomeDeviceAdapter adapter = new HomeDeviceAdapter();
|
||
// 点击模拟设备卡片进入预览页,在线和离线设备均可查看。
|
||
adapter.setOnItemChildClickListener((baseAdapter, view, position) ->
|
||
goTo(mContext, DevicePreviewActivity.class, null));
|
||
String name = getString(R.string.device_home_sample_name);
|
||
List<HomeDeviceEntity> devices = new ArrayList<>();
|
||
devices.add(new HomeDeviceEntity("demo-device-1", name, true, 1200, 180, "128.6"));
|
||
devices.add(new HomeDeviceEntity("demo-device-2", name, true, 1000, 160, "96.4"));
|
||
devices.add(new HomeDeviceEntity("demo-device-3", name, true, 800, 140, "64.2"));
|
||
devices.add(new HomeDeviceEntity("demo-device-4", name, false, 0, 0));
|
||
adapter.setNewData(devices);
|
||
// 复用现有观察器;列表有数据时显示宫格,清空后仍可恢复原来的空状态。
|
||
setDeviceAdapter(adapter);
|
||
}
|
||
|
||
private boolean canUpdateLocationDemo() {
|
||
return isAdded() && getView() != null && viewBinding != null && !isHidden();
|
||
}
|
||
|
||
/** 授权演示接受大致位置权限,实际不调用任何定位服务。 */
|
||
private boolean hasLocationPermission() {
|
||
return isAdded() && (ContextCompat.checkSelfPermission(requireContext(),
|
||
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
|
||
|| ContextCompat.checkSelfPermission(requireContext(),
|
||
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
|
||
}
|
||
|
||
/** 沿用添加设备入口的权限说明弹窗和顶部提示,授权后仅展示模拟结果。 */
|
||
private void requestHomeLocationPermission() {
|
||
if (!canUpdateLocationDemo() || locationPermissionRequestInFlight
|
||
|| bluetoothPermissionRequestInFlight || (dialog != null && dialog.isShowing())) return;
|
||
if (hasLocationPermission()) {
|
||
showLocationDemo();
|
||
return;
|
||
}
|
||
String title = getString(R.string.device_home_location_permission_title);
|
||
String description = getString(R.string.device_home_location_permission_description);
|
||
showDialogtext(null, false, false, title, description,
|
||
getString(R.string.bl_device_permission_deny),
|
||
getString(R.string.bl_device_permission_allow), new OnDialogClick() {
|
||
@Override public void onCancelClick() { }
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
if (!canUpdateLocationDemo()) return;
|
||
if (hasLocationPermission()) {
|
||
showLocationDemo();
|
||
return;
|
||
}
|
||
locationPermissionRequestInFlight = true;
|
||
showPermissionDialog(ContextCompat.getDrawable(requireContext(),
|
||
R.mipmap.img_location), true, title, description);
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
dialog.setCancelable(false);
|
||
// 成对申请以兼容 Android 12+ 的精确/大致位置选择;大致位置也视为成功。
|
||
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
|
||
Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_HOME_LOCATION);
|
||
}
|
||
});
|
||
}
|
||
|
||
/** 仅模拟授权后的城市展示,后续接入真实服务时再替换此处。 */
|
||
private void showLocationDemo() {
|
||
if (!canUpdateLocationDemo() || !hasLocationPermission()) return;
|
||
viewBinding.tvHomeLocation.setText(R.string.device_home_location_demo);
|
||
ToastUtil.showShort(requireContext(), getString(R.string.device_home_location_demo_hint));
|
||
}
|
||
|
||
/** 永久拒绝后引导用户打开应用权限设置,返回后继续授权演示。 */
|
||
private void showLocationSettingsDialog(int messageResId, Intent intent) {
|
||
showDialogtext(null, false, false,
|
||
getString(R.string.device_home_location_permission_title), getString(messageResId),
|
||
getString(R.string.bl_device_permission_deny),
|
||
getString(R.string.bl_device_open_settings), new OnDialogClick() {
|
||
@Override public void onCancelClick() { }
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
if (!canUpdateLocationDemo()) return;
|
||
try {
|
||
waitingForLocationSettings = true;
|
||
locationSettingsLauncher.launch(intent);
|
||
} catch (ActivityNotFoundException e) {
|
||
waitingForLocationSettings = false;
|
||
ToastUtil.showShort(requireContext(),
|
||
getString(R.string.device_home_location_settings_unavailable));
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
/** 添加入口:已有权限直接跳转;缺少权限时先通过基类弹窗说明用途。 */
|
||
private void openBluetoothDeviceList() {
|
||
if (!isAdded() || getView() == null || bluetoothPermissionRequestInFlight
|
||
|| (dialog != null && dialog.isShowing())) {
|
||
return;
|
||
}
|
||
if (BleScanPermissions.hasPermissions(requireContext())) {
|
||
navigateToBluetoothDeviceList();
|
||
return;
|
||
}
|
||
// 说明文案对应实际申请的权限,旧版系统需明确说明位置权限与蓝牙扫描的关系。
|
||
String title = getString(R.string.bl_device_permission_title);
|
||
String description = getString(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
|
||
? R.string.bl_device_permission_description
|
||
: R.string.bl_device_location_permission_description);
|
||
showDialogtext(null, false, false, title, description,
|
||
getString(R.string.bl_device_permission_deny),
|
||
getString(R.string.bl_device_permission_allow), new OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() {
|
||
// 留在设备首页,不触发系统权限申请。
|
||
}
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
if (!isAdded() || getView() == null) return;
|
||
// 说明弹窗显示期间权限可能已变化,真正申请前再检查一次。
|
||
if (BleScanPermissions.hasPermissions(requireContext())) {
|
||
navigateToBluetoothDeviceList();
|
||
return;
|
||
}
|
||
bluetoothPermissionRequestInFlight = true;
|
||
// 顶部说明框在系统权限申请期间保留,收到结果后统一关闭。
|
||
showPermissionDialog(ContextCompat.getDrawable(requireContext(),
|
||
R.mipmap.img_permission_file), true, title, description);
|
||
dialog.setCanceledOnTouchOutside(false);
|
||
dialog.setCancelable(false);
|
||
requestPermissions(BleScanPermissions.requiredPermissions(), REQUEST_EVENT_CODE_10003);
|
||
}
|
||
});
|
||
}
|
||
|
||
/** 只由已有完整权限或完整授权成功的分支调用,不在此方法中再申请权限。 */
|
||
private void navigateToBluetoothDeviceList() {
|
||
if (!isAdded() || getView() == null) return;
|
||
goTo(mContext, BltDeviceListActivity.class, null);
|
||
}
|
||
|
||
/**
|
||
* 后续接入设备数据时,通过此方法设置适配器;notify 数据变化后自动更新空状态。
|
||
*/
|
||
public void setDeviceAdapter(@Nullable RecyclerView.Adapter<?> adapter) {
|
||
mDeviceAdapter = adapter;
|
||
if (getView() != null && viewBinding != null) {
|
||
bindDeviceAdapter();
|
||
}
|
||
}
|
||
|
||
private void bindDeviceAdapter() {
|
||
unregisterDeviceObserver();
|
||
viewBinding.rvDevices.setAdapter(mDeviceAdapter);
|
||
if (mDeviceAdapter != null) {
|
||
mObservedAdapter = mDeviceAdapter;
|
||
mObservedAdapter.registerAdapterDataObserver(mDeviceDataObserver);
|
||
}
|
||
updateDeviceEmptyState();
|
||
}
|
||
|
||
private void updateDeviceEmptyState() {
|
||
if (getView() == null || viewBinding == null) {
|
||
return;
|
||
}
|
||
boolean empty = mDeviceAdapter == null || mDeviceAdapter.getItemCount() == 0;
|
||
viewBinding.svEmptyDevices.setVisibility(empty ? View.VISIBLE : View.GONE);
|
||
viewBinding.rvDevices.setVisibility(empty ? View.GONE : View.VISIBLE);
|
||
}
|
||
|
||
private void unregisterDeviceObserver() {
|
||
if (mObservedAdapter != null) {
|
||
mObservedAdapter.unregisterAdapterDataObserver(mDeviceDataObserver);
|
||
mObservedAdapter = null;
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onDestroyView() {
|
||
// 避免首页视图销毁后遗留顶部说明框或授权等待状态。
|
||
bluetoothPermissionRequestInFlight = false;
|
||
locationPermissionRequestInFlight = false;
|
||
waitingForLocationSettings = false;
|
||
hidePermissionDialog();
|
||
unregisterDeviceObserver();
|
||
if (viewBinding != null) {
|
||
viewBinding.rvDevices.setAdapter(null);
|
||
}
|
||
super.onDestroyView();
|
||
}
|
||
|
||
@Override
|
||
public void onNetChange(int netModel) {
|
||
}
|
||
|
||
@Override
|
||
public void showError(String message) {
|
||
hideDialogLoading();
|
||
if (mContext != null && message != null && !message.isEmpty()) {
|
||
ToastUtil.showShort(mContext, message);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void logOut(BaseBean bean) {
|
||
}
|
||
|
||
@Override
|
||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
|
||
@NonNull int[] grantResults) {
|
||
// 先关闭顶部说明框,再分发结果;通过、拒绝和取消都需要结束说明展示。
|
||
if (requestCode == REQUEST_EVENT_CODE_10003 || requestCode == REQUEST_HOME_LOCATION) {
|
||
hidePermissionDialog();
|
||
}
|
||
// BaseFragment 将结果分发给 EasyPermissions,包含部分授权和拒绝的回调。
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||
if (requestCode == REQUEST_EVENT_CODE_10003) {
|
||
// 系统取消请求时可能返回空结果,同样需要清理等待状态。
|
||
bluetoothPermissionRequestInFlight = false;
|
||
}
|
||
if (requestCode == REQUEST_HOME_LOCATION) locationPermissionRequestInFlight = false;
|
||
}
|
||
|
||
@Override
|
||
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
|
||
if (requestCode == REQUEST_HOME_LOCATION) {
|
||
if (locationPermissionRequestInFlight && canUpdateLocationDemo() && hasLocationPermission()) {
|
||
locationPermissionRequestInFlight = false;
|
||
showLocationDemo();
|
||
}
|
||
return;
|
||
}
|
||
// EasyPermissions 在部分授权时也可能回调此方法,必须复查整组权限才能跳转。
|
||
if (requestCode == REQUEST_EVENT_CODE_10003 && bluetoothPermissionRequestInFlight
|
||
&& isAdded() && getView() != null
|
||
&& BleScanPermissions.hasPermissions(requireContext())) {
|
||
bluetoothPermissionRequestInFlight = false;
|
||
navigateToBluetoothDeviceList();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
|
||
if (requestCode == REQUEST_HOME_LOCATION) {
|
||
// 仅拒绝精确位置但允许大致位置时,也视为授权演示成功。
|
||
if (!locationPermissionRequestInFlight || !canUpdateLocationDemo() || hasLocationPermission()) return;
|
||
locationPermissionRequestInFlight = false;
|
||
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
|
||
showLocationSettingsDialog(R.string.device_home_location_permission_settings,
|
||
new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||
Uri.parse("package:" + requireContext().getPackageName())));
|
||
} else {
|
||
ToastUtil.showShort(requireContext(), getString(R.string.device_home_location_permission_required));
|
||
}
|
||
return;
|
||
}
|
||
if (requestCode != REQUEST_EVENT_CODE_10003 || !isAdded() || getView() == null) return;
|
||
bluetoothPermissionRequestInFlight = false;
|
||
// 永久拒绝后无法再次弹出系统授权框,使用基类弹窗引导用户自行前往应用设置。
|
||
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
|
||
showDialogtext(null, false, false,
|
||
getString(R.string.bl_device_permission_title),
|
||
getString(R.string.bl_device_permission_settings_description),
|
||
getString(R.string.bl_device_permission_deny),
|
||
getString(R.string.bl_device_open_settings), new OnDialogClick() {
|
||
@Override
|
||
public void onCancelClick() { }
|
||
|
||
@Override
|
||
public void onConfirmClick() {
|
||
if (isAdded()) {
|
||
startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||
Uri.parse("package:" + requireContext().getPackageName())));
|
||
}
|
||
}
|
||
});
|
||
} else {
|
||
ToastUtil.showShort(requireContext(), getString(R.string.bl_device_permission_required));
|
||
}
|
||
}
|
||
}
|