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

View File

@ -37,15 +37,8 @@ import java.util.ArrayList;
@SuppressWarnings("LineLength")
public class StartSwapView extends SwapView {
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) {
super(context);
}
@ -83,11 +76,6 @@ public class StartSwapView extends SwapView {
return convertView;
}
}
private SwapService getManager() {
return getActivity().getState();
}
@Nullable /* Emulators typically don't have bluetooth adapters */
@ -163,7 +151,7 @@ public class StartSwapView extends SwapView {
super.onFinishInflate();
if (peerFinderSubscription == null) {
peerFinderSubscription = getManager().scanForPeers().subscribe(onPeerFound);
peerFinderSubscription = getActivity().getSwapService().scanForPeers().subscribe(onPeerFound);
}
uiInitPeers();
@ -238,23 +226,26 @@ public class StartSwapView extends SwapView {
private void uiInitBluetooth() {
if (bluetooth != null) {
textBluetoothVisible = (TextView) findViewById(R.id.bluetooth_visible);
viewBluetoothId = (TextView) findViewById(R.id.device_id_bluetooth);
viewBluetoothId.setText(bluetooth.getName());
viewBluetoothId.setVisibility(bluetooth.isEnabled() ? View.VISIBLE : View.GONE);
int textResource = getManager().isBluetoothDiscoverable() ? R.string.swap_visible_bluetooth : R.string.swap_not_visible_bluetooth;
textBluetoothVisible.setText(textResource);
textBluetoothVisible = findViewById(R.id.bluetooth_visible);
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);
Utils.debugLog(TAG, getManager().isBluetoothDiscoverable()
Utils.debugLog(TAG, getActivity().getSwapService().isBluetoothDiscoverable()
? "Initially marking switch as checked, because Bluetooth is discoverable."
: "Initially marking switch as not-checked, because Bluetooth is not discoverable.");
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 {
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...
} else {
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);
viewBluetoothId.setVisibility(View.GONE);
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.setOnCheckedChangeListener(onWifiSwitchToggled);
setWifiSwitchState(getManager().isBonjourDiscoverable(), true);
setWifiSwitchState(getActivity().getSwapService().isBonjourDiscoverable(), true);
textWifiVisible = (TextView) findViewById(R.id.wifi_visible);
int textResource = getManager().isBonjourDiscoverable() ? R.string.swap_visible_wifi : R.string.swap_not_visible_wifi;
textWifiVisible.setText(textResource);
textWifiVisible = findViewById(R.id.wifi_visible);
if (getActivity().getSwapService().isBonjourDiscoverable()) {
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
// 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) {
if (isChecked) {
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 {
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);
uiUpdateWifiNetwork();

View File

@ -82,7 +82,7 @@ public class SwapSuccessView extends SwapView implements LoaderManager.LoaderCal
@Override
protected void onFinishInflate() {
super.onFinishInflate();
repo = getActivity().getState().getPeerRepo();
repo = getActivity().getSwapService().getPeerRepo();
adapter = new AppListAdapter(getContext(), getContext().getContentResolver().query(
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.");
getActivity().getService().refreshSwap();
getActivity().getSwapService().refreshSwap();
}
private void schedulePollForUpdates() {

View File

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