That fuck shit the fascists are using
at master 138 lines 4.3 kB view raw
1package org.tm.archive.devicetransfer; 2 3import androidx.annotation.NonNull; 4import androidx.lifecycle.LiveData; 5import androidx.lifecycle.ViewModel; 6 7import org.signal.core.util.logging.Log; 8import org.signal.devicetransfer.TransferStatus; 9import org.signal.devicetransfer.WifiDirect; 10import org.tm.archive.util.livedata.LiveDataUtil; 11import org.tm.archive.util.livedata.Store; 12 13/** 14 * Drives and wraps the state of the transfer setup process. 15 */ 16public final class DeviceTransferSetupViewModel extends ViewModel { 17 18 private static final String TAG = Log.tag(DeviceTransferSetupViewModel.class); 19 20 private final Store<DeviceSetupState> store; 21 private final LiveData<DeviceSetupState> distinctStepChanges; 22 23 private boolean shutdown; 24 25 public DeviceTransferSetupViewModel() { 26 this.store = new Store<>(new DeviceSetupState()); 27 this.distinctStepChanges = LiveDataUtil.distinctUntilChanged(this.store.getStateLiveData(), (current, next) -> current.getCurrentSetupStep() == next.getCurrentSetupStep()); 28 } 29 30 public @NonNull LiveData<DeviceSetupState> getState() { 31 return distinctStepChanges; 32 } 33 34 public boolean isNotShutdown() { 35 return !shutdown; 36 } 37 38 public void onTransferEvent(@NonNull TransferStatus event) { 39 if (shutdown) { 40 return; 41 } 42 43 Log.i(TAG, "Handling transferStatus: " + event.getTransferMode()); 44 switch (event.getTransferMode()) { 45 case UNAVAILABLE: 46 case NETWORK_CONNECTED: 47 Log.d(TAG, "Ignore event: " + event.getTransferMode()); 48 break; 49 case READY: 50 case STARTING_UP: 51 store.update(s -> s.updateStep(SetupStep.SETTING_UP)); 52 break; 53 case DISCOVERY: 54 store.update(s -> s.updateStep(SetupStep.WAITING)); 55 break; 56 case VERIFICATION_REQUIRED: 57 store.update(s -> s.updateVerificationRequired(event.getAuthenticationCode())); 58 break; 59 case SERVICE_CONNECTED: 60 store.update(s -> s.updateStep(SetupStep.CONNECTED)); 61 break; 62 case SHUTDOWN: 63 case FAILED: 64 store.update(s -> s.updateStep(SetupStep.ERROR)); 65 break; 66 } 67 } 68 69 public void onLocationPermissionDenied() { 70 Log.i(TAG, "Location permissions denied"); 71 store.update(s -> s.updateStep(SetupStep.PERMISSIONS_DENIED)); 72 } 73 74 public void onWifiDisabled(boolean wifiManagerNotAvailable) { 75 Log.i(TAG, "Wifi disabled manager: " + wifiManagerNotAvailable); 76 store.update(s -> s.updateStep(SetupStep.WIFI_DISABLED)); 77 } 78 79 public void onWifiDirectUnavailable(WifiDirect.AvailableStatus availability) { 80 Log.i(TAG, "Wifi Direct unavailable: " + availability); 81 if (availability == WifiDirect.AvailableStatus.REQUIRED_PERMISSION_NOT_GRANTED) { 82 store.update(s -> s.updateStep(SetupStep.PERMISSIONS_CHECK)); 83 } else { 84 store.update(s -> s.updateStep(SetupStep.WIFI_DIRECT_UNAVAILABLE)); 85 } 86 } 87 88 public void checkPermissions() { 89 Log.d(TAG, "Check for permissions"); 90 shutdown = false; 91 store.update(s -> s.updateStep(SetupStep.PERMISSIONS_CHECK)); 92 } 93 94 public void onPermissionsGranted() { 95 Log.d(TAG, "Permissions granted"); 96 store.update(s -> s.updateStep(SetupStep.LOCATION_CHECK)); 97 } 98 99 public void onLocationEnabled() { 100 Log.d(TAG, "Location enabled"); 101 store.update(s -> s.updateStep(SetupStep.WIFI_CHECK)); 102 } 103 104 public void onLocationDisabled() { 105 Log.d(TAG, "Location disabled"); 106 store.update(s -> s.updateStep(SetupStep.LOCATION_DISABLED)); 107 } 108 109 public void onWifiEnabled() { 110 Log.d(TAG, "Wifi enabled"); 111 store.update(s -> s.updateStep(SetupStep.WIFI_DIRECT_CHECK)); 112 } 113 114 public void onWifiDirectAvailable() { 115 Log.d(TAG, "Wifi direct available"); 116 store.update(s -> s.updateStep(SetupStep.START)); 117 } 118 119 public void onVerified() { 120 store.update(s -> s.updateStep(SetupStep.WAITING_FOR_OTHER_TO_VERIFY)); 121 } 122 123 public void onResume() { 124 store.update(s -> { 125 if (s.getCurrentSetupStep() == SetupStep.WIFI_DISABLED) { 126 return s.updateStep(SetupStep.WIFI_CHECK); 127 } else if (s.getCurrentSetupStep() == SetupStep.LOCATION_DISABLED) { 128 return s.updateStep(SetupStep.LOCATION_CHECK); 129 } 130 return s; 131 }); 132 } 133 134 public void onWaitingTookTooLong() { 135 shutdown = true; 136 store.update(s -> s.updateStep(SetupStep.TROUBLESHOOTING)); 137 } 138}