name all SwapService getters properly: getSwapService()

This also switches to always using getActivity().getSwapService() to make
it easily traceable where that is happening.  It shouldn't be happening in
SwapViews...
This commit is contained in:
Hans-Christoph Steiner 2019-05-16 21:35:35 +02:00
parent e98393f092
commit e7979fca48
4 changed files with 41 additions and 57 deletions

View File

@ -52,10 +52,6 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
} }
private SwapService getState() {
return getActivity().getState();
}
private ListView listView; private ListView listView;
private AppListAdapter adapter; private AppListAdapter adapter;
@ -83,14 +79,14 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall
private void toggleAppSelected(int position) { private void toggleAppSelected(int position) {
Cursor c = (Cursor) adapter.getItem(position); Cursor c = (Cursor) adapter.getItem(position);
String packageName = c.getString(c.getColumnIndex(InstalledAppTable.Cols.Package.NAME)); String packageName = c.getString(c.getColumnIndex(InstalledAppTable.Cols.Package.NAME));
if (getState().hasSelectedPackage(packageName)) { if (getActivity().getSwapService().hasSelectedPackage(packageName)) {
getState().deselectPackage(packageName); getActivity().getSwapService().deselectPackage(packageName);
adapter.updateCheckedIndicatorView(position, false); adapter.updateCheckedIndicatorView(position, false);
} else { } else {
getState().selectPackage(packageName); getActivity().getSwapService().selectPackage(packageName);
adapter.updateCheckedIndicatorView(position, true); adapter.updateCheckedIndicatorView(position, true);
} }
LocalRepoService.create(getContext(), getState().getAppsToSwap()); LocalRepoService.create(getContext(), getActivity().getSwapService().getAppsToSwap());
} }
@Override @Override
@ -117,8 +113,8 @@ public class SelectAppsView extends SwapView implements LoaderManager.LoaderCall
for (int i = 0; i < listView.getCount(); i++) { for (int i = 0; i < listView.getCount(); i++) {
Cursor c = (Cursor) listView.getItemAtPosition(i); Cursor c = (Cursor) listView.getItemAtPosition(i);
String packageName = c.getString(c.getColumnIndex(InstalledAppTable.Cols.Package.NAME)); String packageName = c.getString(c.getColumnIndex(InstalledAppTable.Cols.Package.NAME));
getState().ensureFDroidSelected(); getActivity().getSwapService().ensureFDroidSelected();
for (String selected : getState().getAppsToSwap()) { for (String selected : getActivity().getSwapService().getAppsToSwap()) {
if (TextUtils.equals(packageName, selected)) { if (TextUtils.equals(packageName, selected)) {
listView.setItemChecked(i, true); listView.setItemChecked(i, true);
} }

View File

@ -37,15 +37,8 @@ import java.util.ArrayList;
@SuppressWarnings("LineLength") @SuppressWarnings("LineLength")
public class StartSwapView extends SwapView { public class StartSwapView extends SwapView {
private static final String TAG = "StartSwapView"; private static final String TAG = "StartSwapView";
// TODO: Is there a way to guarantee which of these constructors the inflater will call?
// Especially on different API levels? It would be nice to only have the one which accepts
// a Context, but I'm not sure if that is correct or not. As it stands, this class provides
// constructors which match each of the ones available in the parent class.
// The same is true for the other views in the swap process too.
public StartSwapView(Context context) { public StartSwapView(Context context) {
super(context); super(context);
} }
@ -83,11 +76,6 @@ public class StartSwapView extends SwapView {
return convertView; return convertView;
} }
}
private SwapService getManager() {
return getActivity().getState();
} }
@Nullable /* Emulators typically don't have bluetooth adapters */ @Nullable /* Emulators typically don't have bluetooth adapters */
@ -163,7 +151,7 @@ public class StartSwapView extends SwapView {
super.onFinishInflate(); super.onFinishInflate();
if (peerFinderSubscription == null) { if (peerFinderSubscription == null) {
peerFinderSubscription = getManager().scanForPeers().subscribe(onPeerFound); peerFinderSubscription = getActivity().getSwapService().scanForPeers().subscribe(onPeerFound);
} }
uiInitPeers(); uiInitPeers();
@ -238,23 +226,26 @@ public class StartSwapView extends SwapView {
private void uiInitBluetooth() { private void uiInitBluetooth() {
if (bluetooth != null) { if (bluetooth != null) {
textBluetoothVisible = (TextView) findViewById(R.id.bluetooth_visible);
viewBluetoothId = (TextView) findViewById(R.id.device_id_bluetooth); viewBluetoothId = (TextView) findViewById(R.id.device_id_bluetooth);
viewBluetoothId.setText(bluetooth.getName()); viewBluetoothId.setText(bluetooth.getName());
viewBluetoothId.setVisibility(bluetooth.isEnabled() ? View.VISIBLE : View.GONE); viewBluetoothId.setVisibility(bluetooth.isEnabled() ? View.VISIBLE : View.GONE);
int textResource = getManager().isBluetoothDiscoverable() ? R.string.swap_visible_bluetooth : R.string.swap_not_visible_bluetooth; textBluetoothVisible = findViewById(R.id.bluetooth_visible);
textBluetoothVisible.setText(textResource); if (getActivity().getSwapService().isBluetoothDiscoverable()) {
textBluetoothVisible.setText(R.string.swap_visible_bluetooth);
} else {
textBluetoothVisible.setText(R.string.swap_not_visible_bluetooth);
}
bluetoothSwitch = (SwitchCompat) findViewById(R.id.switch_bluetooth); bluetoothSwitch = (SwitchCompat) findViewById(R.id.switch_bluetooth);
Utils.debugLog(TAG, getManager().isBluetoothDiscoverable() Utils.debugLog(TAG, getActivity().getSwapService().isBluetoothDiscoverable()
? "Initially marking switch as checked, because Bluetooth is discoverable." ? "Initially marking switch as checked, because Bluetooth is discoverable."
: "Initially marking switch as not-checked, because Bluetooth is not discoverable."); : "Initially marking switch as not-checked, because Bluetooth is not discoverable.");
bluetoothSwitch.setOnCheckedChangeListener(onBluetoothSwitchToggled); bluetoothSwitch.setOnCheckedChangeListener(onBluetoothSwitchToggled);
setBluetoothSwitchState(getManager().isBluetoothDiscoverable(), true); setBluetoothSwitchState(getActivity().getSwapService().isBluetoothDiscoverable(), true);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(onBluetoothSwapStateChanged, new IntentFilter(SwapService.BLUETOOTH_STATE_CHANGE)); LocalBroadcastManager.getInstance(getContext()).registerReceiver(onBluetoothSwapStateChanged,
new IntentFilter(SwapService.BLUETOOTH_STATE_CHANGE));
} else { } else {
findViewById(R.id.bluetooth_info).setVisibility(View.GONE); findViewById(R.id.bluetooth_info).setVisibility(View.GONE);
@ -312,7 +303,7 @@ public class StartSwapView extends SwapView {
// TODO: When they deny the request for enabling bluetooth, we need to disable this switch... // TODO: When they deny the request for enabling bluetooth, we need to disable this switch...
} else { } else {
Utils.debugLog(TAG, "Received onCheckChanged(false) for Bluetooth swap, disabling Bluetooth swap."); Utils.debugLog(TAG, "Received onCheckChanged(false) for Bluetooth swap, disabling Bluetooth swap.");
getManager().getBluetoothSwap().stop(); getActivity().getSwapService().getBluetoothSwap().stop();
textBluetoothVisible.setText(R.string.swap_not_visible_bluetooth); textBluetoothVisible.setText(R.string.swap_not_visible_bluetooth);
viewBluetoothId.setVisibility(View.GONE); viewBluetoothId.setVisibility(View.GONE);
Utils.debugLog(TAG, "Received onCheckChanged(false) for Bluetooth swap, Bluetooth swap disabled successfully."); Utils.debugLog(TAG, "Received onCheckChanged(false) for Bluetooth swap, Bluetooth swap disabled successfully.");
@ -328,11 +319,14 @@ public class StartSwapView extends SwapView {
wifiSwitch = (SwitchCompat) findViewById(R.id.switch_wifi); wifiSwitch = (SwitchCompat) findViewById(R.id.switch_wifi);
wifiSwitch.setOnCheckedChangeListener(onWifiSwitchToggled); wifiSwitch.setOnCheckedChangeListener(onWifiSwitchToggled);
setWifiSwitchState(getManager().isBonjourDiscoverable(), true); setWifiSwitchState(getActivity().getSwapService().isBonjourDiscoverable(), true);
textWifiVisible = (TextView) findViewById(R.id.wifi_visible); textWifiVisible = findViewById(R.id.wifi_visible);
int textResource = getManager().isBonjourDiscoverable() ? R.string.swap_visible_wifi : R.string.swap_not_visible_wifi; if (getActivity().getSwapService().isBonjourDiscoverable()) {
textWifiVisible.setText(textResource); textWifiVisible.setText(R.string.swap_visible_wifi);
} else {
textWifiVisible.setText(R.string.swap_not_visible_wifi);
}
// Note that this is only listening for the WifiSwap, whereas we start both the WifiSwap // Note that this is only listening for the WifiSwap, whereas we start both the WifiSwap
// and the Bonjour service at the same time. Technically swap will work fine without // and the Bonjour service at the same time. Technically swap will work fine without
@ -416,10 +410,10 @@ public class StartSwapView extends SwapView {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) { if (isChecked) {
Utils.debugLog(TAG, "Received onCheckChanged(true) for WiFi swap, asking in background thread to ensure WiFi swap is running."); Utils.debugLog(TAG, "Received onCheckChanged(true) for WiFi swap, asking in background thread to ensure WiFi swap is running.");
getManager().getWifiSwap().ensureRunningInBackground(); getActivity().getSwapService().getWifiSwap().ensureRunningInBackground();
} else { } else {
Utils.debugLog(TAG, "Received onCheckChanged(false) for WiFi swap, disabling WiFi swap in background thread."); Utils.debugLog(TAG, "Received onCheckChanged(false) for WiFi swap, disabling WiFi swap in background thread.");
getManager().getWifiSwap().stopInBackground(); getActivity().getSwapService().getWifiSwap().stopInBackground();
} }
SwapService.putWifiVisibleUserPreference(isChecked); SwapService.putWifiVisibleUserPreference(isChecked);
uiUpdateWifiNetwork(); uiUpdateWifiNetwork();

View File

@ -82,7 +82,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
@Override @Override
protected void onFinishInflate() { protected void onFinishInflate() {
super.onFinishInflate(); super.onFinishInflate();
repo = getActivity().getState().getPeerRepo(); repo = getActivity().getSwapService().getPeerRepo();
adapter = new AppListAdapter(getContext(), getContext().getContentResolver().query( adapter = new AppListAdapter(getContext(), getContext().getContentResolver().query(
AppProvider.getRepoUri(repo), AppMetadataTable.Cols.ALL, null, null, null)); AppProvider.getRepoUri(repo), AppMetadataTable.Cols.ALL, null, null, null));
@ -117,7 +117,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
} }
Utils.debugLog(TAG, "Polling swap repo to see if it has any updates."); Utils.debugLog(TAG, "Polling swap repo to see if it has any updates.");
getActivity().getService().refreshSwap(); getActivity().getSwapService().refreshSwap();
} }
private void schedulePollForUpdates() { private void schedulePollForUpdates() {

View File

@ -149,14 +149,14 @@ public class SwapWorkflowActivity extends AppCompatActivity {
private SwapService service; private SwapService service;
@NonNull @NonNull
public SwapService getService() { public SwapService getSwapService() {
return service; return service;
} }
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if (currentView.getLayoutResId() == STEP_INTRO) { if (currentView.getLayoutResId() == STEP_INTRO) {
SwapService.stop(this); // TODO SwapService should always be running, while swap is running SwapService.stop(this);
finish(); finish();
} else { } else {
// TODO: Currently StartSwapView is handleed by the SwapWorkflowActivity as a special case, where // TODO: Currently StartSwapView is handleed by the SwapWorkflowActivity as a special case, where
@ -178,9 +178,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
nextStep = R.layout.swap_join_wifi; nextStep = R.layout.swap_join_wifi;
break; break;
case R.layout.swap_select_apps: case R.layout.swap_select_apps:
// TODO: The STEP_JOIN_WIFI step isn't shown first, need to make it nextStep = getSwapService().isConnectingWithPeer() ? STEP_INTRO : R.layout.swap_join_wifi;
// so that it is, or so that this doesn't go back there.
nextStep = getState().isConnectingWithPeer() ? STEP_INTRO : R.layout.swap_join_wifi;
break; break;
case R.layout.swap_send_fdroid: case R.layout.swap_send_fdroid:
nextStep = STEP_INTRO; nextStep = STEP_INTRO;
@ -473,10 +471,6 @@ public class SwapWorkflowActivity extends AppCompatActivity {
inflateSwapView(currentSwapViewLayoutRes); inflateSwapView(currentSwapViewLayoutRes);
} }
public SwapService getState() {
return service;
}
public void inflateSwapView(@LayoutRes int viewRes) { public void inflateSwapView(@LayoutRes int viewRes) {
container.removeAllViews(); container.removeAllViews();
View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(viewRes, container, false); View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(viewRes, container, false);
@ -522,7 +516,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
public void showIntro() { public void showIntro() {
// If we were previously swapping with a specific client, forget that we were doing that, // If we were previously swapping with a specific client, forget that we were doing that,
// as we are starting over now. // as we are starting over now.
getService().swapWith(null); getSwapService().swapWith(null);
LocalRepoService.create(this); LocalRepoService.create(this);
@ -530,7 +524,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
} }
public void startQrWorkflow() { public void startQrWorkflow() {
if (!getService().isEnabled()) { if (!getSwapService().isEnabled()) {
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setTitle(R.string.not_visible_nearby) .setTitle(R.string.not_visible_nearby)
.setMessage(R.string.not_visible_nearby_description) .setMessage(R.string.not_visible_nearby_description)
@ -574,7 +568,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null if (adapter == null
|| Build.VERSION.SDK_INT >= 23 // TODO make Bluetooth work with content:// URIs || Build.VERSION.SDK_INT >= 23 // TODO make Bluetooth work with content:// URIs
|| (!adapter.isEnabled() && getService().getWifiSwap().isConnected())) { || (!adapter.isEnabled() && getSwapService().getWifiSwap().isConnected())) {
inflateSwapView(R.layout.swap_send_fdroid); inflateSwapView(R.layout.swap_send_fdroid);
} else { } else {
sendFDroidBluetooth(); sendFDroidBluetooth();
@ -610,7 +604,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
if (hasPreparedLocalRepo) { if (hasPreparedLocalRepo) {
onLocalRepoPrepared(); onLocalRepoPrepared();
} else { } else {
LocalRepoService.create(this, getService().getAppsToSwap()); LocalRepoService.create(this, getSwapService().getAppsToSwap());
currentSwapViewLayoutRes = R.layout.swap_connecting; currentSwapViewLayoutRes = R.layout.swap_connecting;
inflateSwapView(R.layout.swap_connecting); inflateSwapView(R.layout.swap_connecting);
} }
@ -629,7 +623,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
public void onLocalRepoPrepared() { public void onLocalRepoPrepared() {
// TODO ditch this, use a message from LocalRepoService. Maybe? // TODO ditch this, use a message from LocalRepoService. Maybe?
hasPreparedLocalRepo = true; hasPreparedLocalRepo = true;
if (getService().isConnectingWithPeer()) { if (getSwapService().isConnectingWithPeer()) {
startSwappingWithPeer(); startSwappingWithPeer();
} else if (!attemptToShowNfc()) { } else if (!attemptToShowNfc()) {
inflateSwapView(R.layout.swap_wifi_qr); inflateSwapView(R.layout.swap_wifi_qr);
@ -637,7 +631,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
} }
private void startSwappingWithPeer() { private void startSwappingWithPeer() {
getService().connectToPeer(); getSwapService().connectToPeer();
inflateSwapView(R.layout.swap_connecting); inflateSwapView(R.layout.swap_connecting);
} }
@ -659,7 +653,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
} }
public void swapWith(Peer peer) { public void swapWith(Peer peer) {
getService().swapWith(peer); getSwapService().swapWith(peer);
inflateSwapView(R.layout.swap_select_apps); inflateSwapView(R.layout.swap_select_apps);
} }
@ -679,7 +673,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
// can or cannot be in STEP_INTRO with a full blown repo ready to swap. // can or cannot be in STEP_INTRO with a full blown repo ready to swap.
swapWith(peer); swapWith(peer);
} else { } else {
getService().swapWith(repoConfig.toPeer()); getSwapService().swapWith(repoConfig.toPeer());
startSwappingWithPeer(); startSwappingWithPeer();
} }
} }
@ -728,7 +722,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
if (resultCode != RESULT_CANCELED) { if (resultCode != RESULT_CANCELED) {
Utils.debugLog(TAG, "User made Bluetooth discoverable, will proceed to start bluetooth server."); Utils.debugLog(TAG, "User made Bluetooth discoverable, will proceed to start bluetooth server.");
getState().getBluetoothSwap().startInBackground(); // TODO replace with Intent to SwapService getSwapService().getBluetoothSwap().startInBackground(); // TODO replace with Intent to SwapService
} else { } else {
Utils.debugLog(TAG, "User chose not to make Bluetooth discoverable, so doing nothing"); Utils.debugLog(TAG, "User chose not to make Bluetooth discoverable, so doing nothing");
SwapService.putBluetoothVisibleUserPreference(false); SwapService.putBluetoothVisibleUserPreference(false);