Browse Source

Merge pull request 'Adds getConfig API Call.' (#5) from vpn_configuration into dev

Reviewed-on: https://git.bubblev.org/bubblev/bubble-droid/pulls/5
pull/6/head
Gegham Kurghinyan 4 years ago
parent
commit
f7363d2f81
3 changed files with 54 additions and 17 deletions
  1. +2
    -0
      ui/src/main/java/com/wireguard/android/api/ApiConstants.java
  2. +4
    -0
      ui/src/main/java/com/wireguard/android/api/network/NetworkBoundStatusResource.java
  3. +48
    -17
      ui/src/main/java/com/wireguard/android/repository/DataRepository.java

+ 2
- 0
ui/src/main/java/com/wireguard/android/api/ApiConstants.java View File

@@ -5,6 +5,8 @@ public class ApiConstants {
public static final String LOGIN_URL = "auth/login";
public static final String ALL_DEVICES_URL = "me/devices";
public static final String ADD_DEVICE_URL = "me/devices";
public static final String CONFIG_DEVICE_URL = "me/devices/";
public static final String CONFIG_VPN_URL = "/vpn/vpn.conf";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AUTHORIZATION_HEADER = "X-Bubble-Session";


+ 4
- 0
ui/src/main/java/com/wireguard/android/api/network/NetworkBoundStatusResource.java View File

@@ -21,6 +21,10 @@ public abstract class NetworkBoundStatusResource<T> {
mutableLiveData.setValue(value);
}

public void postMutableLiveData(StatusResource<T> value) {
mutableLiveData.postValue(value);
}

public final MutableLiveData<StatusResource<T>> getMutableLiveData() {
return mutableLiveData;
}


+ 48
- 17
ui/src/main/java/com/wireguard/android/repository/DataRepository.java View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.provider.Settings.Secure;

import com.wireguard.android.api.ApiConstants;
import com.wireguard.android.api.network.ClientApi;
import com.wireguard.android.api.network.ClientService;
@@ -12,24 +13,35 @@ import com.wireguard.android.model.Device;
import com.wireguard.android.model.User;
import com.wireguard.android.resource.StatusResource;
import com.wireguard.android.util.UserStore;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import androidx.lifecycle.MutableLiveData;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Call;

public class DataRepository {
private static volatile DataRepository instance;
private ClientApi clientApi;
private CompositeDisposable compositeDisposable;
private final OkHttpClient client = new OkHttpClient();

public static final String NO_INTERNET_CONNECTION = "no_internet_connection";
private static final String SEPARATOR = ":";
private static final String SPACE = " ";
private static final String DELIMITER = "\\A";
private static final int ANDROID_ID = 1;

private DataRepository() {
@@ -65,9 +77,8 @@ public class DataRepository {
UserStore.getInstance(context).setToken(user.getToken());
if (!isDeviceLoggedIn(context)) {
addDevice(context);
}
else {
getAllDevices(context);
} else {
getAllDevices(context);
}
}, throwable -> {
setMutableLiveData(StatusResource.error(throwable.getMessage()));
@@ -77,12 +88,12 @@ public class DataRepository {

private void getAllDevices(final Context context) {
final String token = UserStore.getInstance(context).getToken();
final HashMap<String,String> header = new HashMap<>();
header.put(ApiConstants.AUTHORIZATION_HEADER,token);
Disposable disposableAllDevices = clientApi.getAllDevices(header)
final HashMap<String, String> header = new HashMap<>();
header.put(ApiConstants.AUTHORIZATION_HEADER, token);
Disposable disposableAllDevices = clientApi.getAllDevices(header)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(listDevices->{
.subscribe(listDevices -> {
boolean hasDevice = false;
for (Device item : listDevices) {
if (UserStore.getInstance(context).getDeviceID().equals(item.getUuid())) {
@@ -91,10 +102,10 @@ public class DataRepository {
break;
}
}
if(!hasDevice) {
if (!hasDevice) {
addDevice(context);
}
},throwable -> {
}, throwable -> {

});
compositeDisposable.add(disposableAllDevices);
@@ -124,7 +135,7 @@ public class DataRepository {
UserStore.getInstance(context).setDeviceName(device.getName());
UserStore.getInstance(context).setDeviceID(device.getUuid());
hasDevice = true;
setMutableLiveData(StatusResource.success());
getConfig(context);
break;
} else {
final String[] itemDevice = device.getName().split(SEPARATOR);
@@ -149,13 +160,12 @@ public class DataRepository {
.subscribe(device -> {
UserStore.getInstance(context).setDeviceName(device.getName());
UserStore.getInstance(context).setDeviceID(device.getUuid());
setMutableLiveData(StatusResource.success());
getConfig(context);
}, throwable -> {
setMutableLiveData(StatusResource.error(throwable.getMessage()));
});
compositeDisposable.add(disposableAddDevice);
}
else {
} else {
for (int i = (arrayListDevicesName.size() - 1); i >= arrayListDevicesName.size() - 1; i--) {
if (arrayListDevicesName.get(i).contains(brandModel)) {
final char[] arr = arrayListDevicesName.get(i).toCharArray();
@@ -183,14 +193,14 @@ public class DataRepository {
final HashMap<String, String> body = new HashMap<>();
body.put(ApiConstants.DEVICE_NAME, brandModel);
body.put(ApiConstants.DEVICE_TYPE, "android");
Disposable disposableAddDevice = clientApi.addDevice(header,body)
Disposable disposableAddDevice = clientApi.addDevice(header, body)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(device -> {
UserStore.getInstance(context).setDeviceName(device.getName());
UserStore.getInstance(context).setDeviceID(device.getUuid());
setMutableLiveData(StatusResource.success());
},throwable -> {
getConfig(context);
}, throwable -> {
setMutableLiveData(StatusResource.error(throwable.getMessage()));
});
compositeDisposable.add(disposableAddDevice);
@@ -202,6 +212,27 @@ public class DataRepository {
});
compositeDisposable.add(disposableAllDevices);
}

private void getConfig(Context context) {
final String deviceID = UserStore.getInstance(context).getDeviceID();
final String token = UserStore.getInstance(context).getToken();
Request request = new Request.Builder()
.url(ApiConstants.BASE_URL + ApiConstants.CONFIG_DEVICE_URL + deviceID + ApiConstants.CONFIG_VPN_URL)
.addHeader(ApiConstants.AUTHORIZATION_HEADER, token)
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(final okhttp3.Call call, final IOException e) {
setMutableLiveData(StatusResource.error(e.getMessage()));
}

@Override public void onResponse(final okhttp3.Call call, final Response response) throws IOException {
final InputStream inputStream = response.body().byteStream();
final Scanner scanner = new Scanner(inputStream).useDelimiter(DELIMITER);
final String data = scanner.hasNext() ? scanner.next() : "";
postMutableLiveData(StatusResource.success());
}
});
}
}.getMutableLiveData();
}

@@ -241,7 +272,7 @@ public class DataRepository {
return Build.MODEL;
}

public void clearDisposable(){
public void clearDisposable() {
compositeDisposable.clear();
}
}

Loading…
Cancel
Save