WIP: More cleaning up of swap process, unbinding service, and investigating getting rid of ConnectSwapActivity.
This commit is contained in:
parent
0b1e2732c2
commit
4e4d3ec3f9
@ -30,4 +30,6 @@
|
|||||||
tools:text="Downloading index from http://10.0.0.4:8888/fdroid/repo"
|
tools:text="Downloading index from http://10.0.0.4:8888/fdroid/repo"
|
||||||
android:padding="30dp" />
|
android:padding="30dp" />
|
||||||
|
|
||||||
|
<!-- TODO: Add button for "Retry" and show an error message when failed connecting. -->
|
||||||
|
|
||||||
</org.fdroid.fdroid.views.swap.SwapConnecting>
|
</org.fdroid.fdroid.views.swap.SwapConnecting>
|
@ -10,6 +10,7 @@ import android.content.Intent;
|
|||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.net.http.AndroidHttpClient;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.annotation.IntDef;
|
import android.support.annotation.IntDef;
|
||||||
@ -20,11 +21,18 @@ import android.support.v4.content.LocalBroadcastManager;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.NameValuePair;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
import org.fdroid.fdroid.FDroidApp;
|
||||||
import org.fdroid.fdroid.Preferences;
|
import org.fdroid.fdroid.Preferences;
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
import org.fdroid.fdroid.UpdateService;
|
import org.fdroid.fdroid.UpdateService;
|
||||||
|
import org.fdroid.fdroid.Utils;
|
||||||
import org.fdroid.fdroid.data.App;
|
import org.fdroid.fdroid.data.App;
|
||||||
|
import org.fdroid.fdroid.data.NewRepoConfig;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
import org.fdroid.fdroid.data.RepoProvider;
|
import org.fdroid.fdroid.data.RepoProvider;
|
||||||
import org.fdroid.fdroid.localrepo.peers.BluetoothFinder;
|
import org.fdroid.fdroid.localrepo.peers.BluetoothFinder;
|
||||||
@ -37,11 +45,14 @@ import org.fdroid.fdroid.localrepo.type.WebServerType;
|
|||||||
import org.fdroid.fdroid.net.WifiStateChangeService;
|
import org.fdroid.fdroid.net.WifiStateChangeService;
|
||||||
import org.fdroid.fdroid.views.swap.SwapWorkflowActivity;
|
import org.fdroid.fdroid.views.swap.SwapWorkflowActivity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
@ -134,11 +145,11 @@ public class SwapService extends Service {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public UpdateService.UpdateReceiver refreshSwap() {
|
public UpdateService.UpdateReceiver refreshSwap() {
|
||||||
return this.peer != null ? connectTo(peer) : null;
|
return this.peer != null ? connectTo(peer, false) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public UpdateService.UpdateReceiver connectTo(@NonNull Peer peer) {
|
public UpdateService.UpdateReceiver connectTo(@NonNull Peer peer, boolean requestSwapBack) {
|
||||||
if (peer != this.peer) {
|
if (peer != this.peer) {
|
||||||
Log.e(TAG, "Oops, got a different peer to swap with than initially planned.");
|
Log.e(TAG, "Oops, got a different peer to swap with than initially planned.");
|
||||||
}
|
}
|
||||||
@ -148,30 +159,34 @@ public class SwapService extends Service {
|
|||||||
// Only ask server to swap with us, if we are actually running a local repo service.
|
// Only ask server to swap with us, if we are actually running a local repo service.
|
||||||
// It is possible to have a swap initiated without first starting a swap, in which
|
// It is possible to have a swap initiated without first starting a swap, in which
|
||||||
// case swapping back is pointless.
|
// case swapping back is pointless.
|
||||||
/*if (!newRepoConfig.preventFurtherSwaps() && isEnabled()) {
|
if (isEnabled()) {
|
||||||
askServerToSwapWithUs();
|
askServerToSwapWithUs(peerRepo);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
return UpdateService.updateRepoNow(peer.getRepoAddress(), this, false);
|
return UpdateService.updateRepoNow(peer.getRepoAddress(), this, false);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
private void askServerToSwapWithUs() {
|
private void askServerToSwapWithUs(final Repo repo) {
|
||||||
if (!newRepoConfig.isValidRepo()) {
|
askServerToSwapWithUs(repo.address);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void askServerToSwapWithUs(final NewRepoConfig config) {
|
||||||
|
askServerToSwapWithUs(config.getRepoUriString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void askServerToSwapWithUs(final String address) {
|
||||||
new AsyncTask<Void, Void, Void>() {
|
new AsyncTask<Void, Void, Void>() {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... args) {
|
protected Void doInBackground(Void... args) {
|
||||||
Uri repoUri = newRepoConfig.getRepoUri();
|
Uri repoUri = Uri.parse(address);
|
||||||
String swapBackUri = Utils.getLocalRepoUri(FDroidApp.repo).toString();
|
String swapBackUri = Utils.getLocalRepoUri(FDroidApp.repo).toString();
|
||||||
|
|
||||||
AndroidHttpClient client = AndroidHttpClient.newInstance("F-Droid", ConnectSwapActivity.this);
|
AndroidHttpClient client = AndroidHttpClient.newInstance("F-Droid", SwapService.this);
|
||||||
HttpPost request = new HttpPost("/request-swap");
|
HttpPost request = new HttpPost("/request-swap");
|
||||||
HttpHost host = new HttpHost(repoUri.getHost(), repoUri.getPort(), repoUri.getScheme());
|
HttpHost host = new HttpHost(repoUri.getHost(), repoUri.getPort(), repoUri.getScheme());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Log.d(TAG, "Asking server at " + newRepoConfig.getRepoUriString() + " to swap with us in return (by POSTing to \"/request-swap\" with repo \"" + swapBackUri + "\")...");
|
Log.d(TAG, "Asking server at " + address + " to swap with us in return (by POSTing to \"/request-swap\" with repo \"" + swapBackUri + "\")...");
|
||||||
populatePostParams(swapBackUri, request);
|
populatePostParams(swapBackUri, request);
|
||||||
client.execute(host, request);
|
client.execute(host, request);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -191,19 +206,21 @@ public class SwapService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void notifyOfErrorOnUiThread() {
|
private void notifyOfErrorOnUiThread() {
|
||||||
runOnUiThread(new Runnable() {
|
// TODO: Broadcast error message so that whoever wants to can display a relevant
|
||||||
|
// message in the UI. This service doesn't understand the concept of UI.
|
||||||
|
/*runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
ConnectSwapActivity.this,
|
SwapService.this,
|
||||||
R.string.swap_reciprocate_failed,
|
R.string.swap_reciprocate_failed,
|
||||||
Toast.LENGTH_LONG
|
Toast.LENGTH_LONG
|
||||||
).show();
|
).show();
|
||||||
}
|
}
|
||||||
});
|
});*/
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
}*/
|
}
|
||||||
|
|
||||||
private Repo ensureRepoExists(@NonNull Peer peer) {
|
private Repo ensureRepoExists(@NonNull Peer peer) {
|
||||||
// TODO: newRepoConfig.getParsedUri() will include a fingerprint, which may not match with
|
// TODO: newRepoConfig.getParsedUri() will include a fingerprint, which may not match with
|
||||||
|
@ -167,6 +167,7 @@ public class WifiStateChangeService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
((SwapService.Binder)service).getService().restartIfEnabled();
|
((SwapService.Binder)service).getService().restartIfEnabled();
|
||||||
|
unbindService(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -6,8 +6,6 @@ import android.content.ContentValues;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.net.http.AndroidHttpClient;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
@ -15,32 +13,18 @@ import android.support.v7.app.ActionBarActivity;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.apache.http.HttpHost;
|
|
||||||
import org.apache.http.NameValuePair;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
|
||||||
import org.fdroid.fdroid.ProgressListener;
|
import org.fdroid.fdroid.ProgressListener;
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
import org.fdroid.fdroid.UpdateService;
|
import org.fdroid.fdroid.UpdateService;
|
||||||
import org.fdroid.fdroid.Utils;
|
|
||||||
import org.fdroid.fdroid.data.NewRepoConfig;
|
import org.fdroid.fdroid.data.NewRepoConfig;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
import org.fdroid.fdroid.data.RepoProvider;
|
import org.fdroid.fdroid.data.RepoProvider;
|
||||||
import org.fdroid.fdroid.localrepo.SwapService;
|
import org.fdroid.fdroid.localrepo.SwapService;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ConnectSwapActivity extends ActionBarActivity implements ProgressListener {
|
public class ConnectSwapActivity extends ActionBarActivity implements ProgressListener {
|
||||||
private static final String TAG = "ConnectSwapActivity";
|
|
||||||
|
|
||||||
private static final String STATE_CONFIRM = "startSwap";
|
private static final String TAG = "ConnectSwapActivity";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When connecting to a swap, we then go and initiate a connection with that
|
* When connecting to a swap, we then go and initiate a connection with that
|
||||||
@ -167,7 +151,7 @@ public class ConnectSwapActivity extends ActionBarActivity implements ProgressLi
|
|||||||
*/
|
*/
|
||||||
private void attemptSwapBack() {
|
private void attemptSwapBack() {
|
||||||
|
|
||||||
if (newRepoConfig.preventFurtherSwaps()) {
|
if (!newRepoConfig.isValidRepo() || newRepoConfig.preventFurtherSwaps()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,8 +160,9 @@ public class ConnectSwapActivity extends ActionBarActivity implements ProgressLi
|
|||||||
public void onServiceConnected(ComponentName name, IBinder binder) {
|
public void onServiceConnected(ComponentName name, IBinder binder) {
|
||||||
SwapService service = ((SwapService.Binder) binder).getService();
|
SwapService service = ((SwapService.Binder) binder).getService();
|
||||||
if (service.isEnabled()) {
|
if (service.isEnabled()) {
|
||||||
askServerToSwapWithUs();
|
service.askServerToSwapWithUs(newRepoConfig);
|
||||||
}
|
}
|
||||||
|
unbindService(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -187,54 +172,4 @@ public class ConnectSwapActivity extends ActionBarActivity implements ProgressLi
|
|||||||
Intent intent = new Intent(this, SwapService.class);
|
Intent intent = new Intent(this, SwapService.class);
|
||||||
bindService(intent, connection, BIND_AUTO_CREATE);
|
bindService(intent, connection, BIND_AUTO_CREATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void askServerToSwapWithUs() {
|
|
||||||
if (!newRepoConfig.isValidRepo()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
new AsyncTask<Void, Void, Void>() {
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... args) {
|
|
||||||
Uri repoUri = newRepoConfig.getRepoUri();
|
|
||||||
String swapBackUri = Utils.getLocalRepoUri(FDroidApp.repo).toString();
|
|
||||||
|
|
||||||
AndroidHttpClient client = AndroidHttpClient.newInstance("F-Droid", ConnectSwapActivity.this);
|
|
||||||
HttpPost request = new HttpPost("/request-swap");
|
|
||||||
HttpHost host = new HttpHost(repoUri.getHost(), repoUri.getPort(), repoUri.getScheme());
|
|
||||||
|
|
||||||
try {
|
|
||||||
Log.d(TAG, "Asking server at " + newRepoConfig.getRepoUriString() + " to swap with us in return (by POSTing to \"/request-swap\" with repo \"" + swapBackUri + "\")...");
|
|
||||||
populatePostParams(swapBackUri, request);
|
|
||||||
client.execute(host, request);
|
|
||||||
} catch (IOException e) {
|
|
||||||
notifyOfErrorOnUiThread();
|
|
||||||
Log.e(TAG, "Error while asking server to swap with us: " + e.getMessage());
|
|
||||||
} finally {
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void populatePostParams(String swapBackUri, HttpPost request) throws UnsupportedEncodingException {
|
|
||||||
List<NameValuePair> params = new ArrayList<>();
|
|
||||||
params.add(new BasicNameValuePair("repo", swapBackUri));
|
|
||||||
UrlEncodedFormEntity encodedParams = new UrlEncodedFormEntity(params);
|
|
||||||
request.setEntity(encodedParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyOfErrorOnUiThread() {
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Toast.makeText(
|
|
||||||
ConnectSwapActivity.this,
|
|
||||||
R.string.swap_reciprocate_failed,
|
|
||||||
Toast.LENGTH_LONG
|
|
||||||
).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}.execute();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
getContext().registerReceiver(new BroadcastReceiver() {
|
LocalBroadcastManager.getInstance(getContext()).registerReceiver(new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Peer peer = intent.getParcelableExtra(SwapService.EXTRA_PEER);
|
Peer peer = intent.getParcelableExtra(SwapService.EXTRA_PEER);
|
||||||
|
@ -64,7 +64,7 @@ public class SwapConnecting extends LinearLayout implements SwapWorkflowActivity
|
|||||||
String heading = getContext().getString(R.string.status_connecting_to_repo, getActivity().getState().getPeer().getName());
|
String heading = getContext().getString(R.string.status_connecting_to_repo, getActivity().getState().getPeer().getName());
|
||||||
((TextView) findViewById(R.id.heading)).setText(heading);
|
((TextView) findViewById(R.id.heading)).setText(heading);
|
||||||
|
|
||||||
UpdateService.UpdateReceiver receiver = getManager().connectTo(peer);
|
UpdateService.UpdateReceiver receiver = getManager().connectTo(peer, true);
|
||||||
|
|
||||||
receiver.hideDialog();
|
receiver.hideDialog();
|
||||||
receiver.setListener(new ProgressListener() {
|
receiver.setListener(new ProgressListener() {
|
||||||
@ -117,7 +117,7 @@ public class SwapConnecting extends LinearLayout implements SwapWorkflowActivity
|
|||||||
|
|
||||||
@ColorRes
|
@ColorRes
|
||||||
public int getToolbarColour() {
|
public int getToolbarColour() {
|
||||||
return getResources().getColor(R.color.swap_blue);
|
return getResources().getColor(R.color.swap_bright_blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,21 +74,8 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
private boolean hasPreparedLocalRepo = false;
|
private boolean hasPreparedLocalRepo = false;
|
||||||
private PrepareSwapRepo updateSwappableAppsTask = null;
|
private PrepareSwapRepo updateSwappableAppsTask = null;
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private SwapService service = null;
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public SwapService getService() {
|
private final ServiceConnection serviceConnection = new ServiceConnection() {
|
||||||
if (service == null) {
|
|
||||||
// *Slightly* more informative than a null-pointer error that would otherwise happen.
|
|
||||||
throw new IllegalStateException("Trying to access swap service before it was initialized.");
|
|
||||||
}
|
|
||||||
return service;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupService() {
|
|
||||||
|
|
||||||
ServiceConnection serviceConnection = new ServiceConnection() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName className, IBinder binder) {
|
public void onServiceConnected(ComponentName className, IBinder binder) {
|
||||||
Log.d(TAG, "Swap service connected, enabling SwapManager to communicate with SwapService.");
|
Log.d(TAG, "Swap service connected, enabling SwapManager to communicate with SwapService.");
|
||||||
@ -104,14 +91,16 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// The server should not be doing anything or occupying any (noticeable) resources
|
@Nullable
|
||||||
// until we actually ask it to enable swapping. Therefore, we will start it nice and
|
private SwapService service = null;
|
||||||
// early so we don't have to wait until it is connected later.
|
|
||||||
Intent service = new Intent(this, SwapService.class);
|
|
||||||
if (bindService(service, serviceConnection, Context.BIND_AUTO_CREATE)) {
|
|
||||||
startService(service);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public SwapService getService() {
|
||||||
|
if (service == null) {
|
||||||
|
// *Slightly* more informative than a null-pointer error that would otherwise happen.
|
||||||
|
throw new IllegalStateException("Trying to access swap service before it was initialized.");
|
||||||
|
}
|
||||||
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -129,7 +118,13 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setupService();
|
// The server should not be doing anything or occupying any (noticeable) resources
|
||||||
|
// until we actually ask it to enable swapping. Therefore, we will start it nice and
|
||||||
|
// early so we don't have to wait until it is connected later.
|
||||||
|
Intent service = new Intent(this, SwapService.class);
|
||||||
|
if (bindService(service, serviceConnection, Context.BIND_AUTO_CREATE)) {
|
||||||
|
startService(service);
|
||||||
|
}
|
||||||
|
|
||||||
setContentView(R.layout.swap_activity);
|
setContentView(R.layout.swap_activity);
|
||||||
|
|
||||||
@ -140,6 +135,12 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
container = (ViewGroup) findViewById(R.id.fragment_container);
|
container = (ViewGroup) findViewById(R.id.fragment_container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
unbindService(serviceConnection);
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
menu.clear();
|
menu.clear();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user