Bläddra i källkod

Implement the progress meter.

pull/24/head
Mushegh Sahakyan 4 år sedan
förälder
incheckning
8797561486
9 ändrade filer med 323 tillägg och 3 borttagningar
  1. +30
    -1
      ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java
  2. +1
    -0
      ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java
  3. +4
    -0
      ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java
  4. +76
    -0
      ui/src/main/java/com/getbubblenow/android/fragment/ProgressLoadingDialogFragment.java
  5. +78
    -0
      ui/src/main/java/com/getbubblenow/android/model/NetworkStatus.java
  6. +81
    -2
      ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java
  7. +12
    -0
      ui/src/main/java/com/getbubblenow/android/viewmodel/BaseViewModel.java
  8. +12
    -0
      ui/src/main/java/com/getbubblenow/android/viewmodel/ProgressLoadingViewModel.java
  9. +29
    -0
      ui/src/main/res/layout/progress_loading_dialog.xml

+ 30
- 1
ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java Visa fil

@@ -8,30 +8,48 @@ import android.view.View;
import android.widget.Toast;

import com.getbubblenow.android.R;
import com.getbubblenow.android.api.ApiConstants;
import com.getbubblenow.android.fragment.ErrorDialogFragment;
import com.getbubblenow.android.fragment.LoadingDialogFragment;
import com.getbubblenow.android.fragment.ErrorDialogFragment;
import com.getbubblenow.android.fragment.LoadingDialogFragment;
import com.getbubblenow.android.fragment.ProgressLoadingDialogFragment;
import com.getbubblenow.android.repository.DataRepository;
import com.getbubblenow.android.viewmodel.BaseViewModel;
import com.getbubblenow.android.viewmodel.LoginViewModel;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;

public class BaseActivityBubble extends AppCompatActivity {
public class BaseActivityBubble extends AppCompatActivity implements DataRepository.ShowNetworkProgressInterface {

public static final String LOADING_TAG = "loading_tag";
public static final String NO_CONNECTION_TAG = "no_connection_tag";
public static final String ERROR_TAG = "error_tag";
public static final String PROGRESS_TAG = "progress_tag";
public static final String RATE_TAG = "rate tag";
private final long LOADER_DELAY = 1000;

private BaseViewModel baseViewModel;
private LoadingDialogFragment loadingDialog;
private ErrorDialogFragment errorDialog;
private ProgressLoadingDialogFragment progressLoadingDialog;
private boolean showDialog = true;
private boolean showProgress = false;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (DataRepository.getRepositoryInstance() == null) {
DataRepository.buildRepositoryInstance(this, ApiConstants.BOOTSTRAP_URL);
} else {
DataRepository.getRepositoryInstance().buildClientService(ApiConstants.BOOTSTRAP_URL);
}
baseViewModel = new ViewModelProvider(this).get(BaseViewModel.class);
progressLoadingDialog = ProgressLoadingDialogFragment.newInstance();
baseViewModel.setShowNetworkProgressInterface(this);
}


@@ -88,4 +106,15 @@ public class BaseActivityBubble extends AppCompatActivity {
getSupportFragmentManager().beginTransaction().add(errorDialog,ERROR_TAG).commitAllowingStateLoss();
}

@Override public void showProgress() {
showProgress = true;
getSupportFragmentManager().beginTransaction().add(progressLoadingDialog,PROGRESS_TAG).commitAllowingStateLoss();
}

@Override public void closeProgress() {
if(showProgress) {
progressLoadingDialog.dismissAllowingStateLoss();
showProgress = false;
}
}
}

+ 1
- 0
ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java Visa fil

@@ -16,4 +16,5 @@ public class ApiConstants {
public static final String DEVICE_NAME = "name";
public static final String DEVICE_TYPE = "deviceType";
public static final String CERTIFICATE_URL = "auth/cacert?deviceType=android";
public static final String NETWORK_STATUS = "users/{userId}/networks/{networkId}/actions/status";
}

+ 4
- 0
ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java Visa fil

@@ -2,6 +2,7 @@ package com.getbubblenow.android.api.network;

import com.getbubblenow.android.model.Device;
import com.getbubblenow.android.model.Network;
import com.getbubblenow.android.model.NetworkStatus;
import com.getbubblenow.android.model.Sages;
import com.getbubblenow.android.model.User;
import com.getbubblenow.android.api.ApiConstants;
@@ -47,4 +48,7 @@ public interface ClientApi {

@GET(ApiConstants.NODE_BASE_URI)
Single<List<Network>> getNodeBaseURI(@HeaderMap HashMap<String,String> header);

@GET(ApiConstants.NETWORK_STATUS)
Single<List<NetworkStatus>> getNetworkState(@Path("userId") String userId , @Path("networkId") String networkId , @HeaderMap HashMap<String,String> header);
}

+ 76
- 0
ui/src/main/java/com/getbubblenow/android/fragment/ProgressLoadingDialogFragment.java Visa fil

@@ -0,0 +1,76 @@
package com.getbubblenow.android.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.getbubblenow.android.R;
import com.getbubblenow.android.activity.BaseActivityBubble;
import com.getbubblenow.android.repository.DataRepository;
import com.getbubblenow.android.viewmodel.ProgressLoadingViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;

public class ProgressLoadingDialogFragment extends DialogFragment implements DataRepository.NetworkProgressStatusInterface {

private ProgressBar progressLoading;
private ProgressLoadingViewModel progressLoadingViewModel;
private TextView progressTitle;

public static ProgressLoadingDialogFragment newInstance() {
ProgressLoadingDialogFragment fragment = new ProgressLoadingDialogFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity(), getTheme()) {
@Override
public void onBackPressed() {
if (getActivity().getSupportFragmentManager().findFragmentByTag(BaseActivityBubble.PROGRESS_TAG) != null) {
getActivity().onBackPressed();
}
super.onBackPressed();
}
};
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.progress_loading_dialog, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressLoadingViewModel = new ViewModelProvider(this).get(ProgressLoadingViewModel.class);
progressLoading = view.findViewById(R.id.network_progress);
progressTitle = view.findViewById(R.id.progress_title);
progressLoadingViewModel.setNetworkProgressStateInterface(this);
}

@Override public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override public void setProgress(final long percent, final String progressTitle) {
progressLoading.setProgress(Integer.parseInt(String.valueOf(percent)));
this.progressTitle.setText(percent+":"+progressTitle);
}
}

+ 78
- 0
ui/src/main/java/com/getbubblenow/android/model/NetworkStatus.java Visa fil

@@ -0,0 +1,78 @@
package com.getbubblenow.android.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NetworkStatus {

@SerializedName("account")
@Expose
private String account;
@SerializedName("network")
@Expose
private String network;
@SerializedName("pattern")
@Expose
private String pattern;
@SerializedName("match")
@Expose
private String match;
@SerializedName("percent")
@Expose
private long percent;
@SerializedName("messageKey")
@Expose
private String messageKey;
@SerializedName("details")
@Expose
private String details;

public String getPattern() {
return pattern;
}

public void setPattern(final String pattern) {
this.pattern = pattern;
}

public String getMatch() {
return match;
}

public void setMatch(final String match) {
this.match = match;
}

public String getDetails() {
return details;
}

public void setDetails(final String details) {
this.details = details;
}

public String getAccount() {
return account;
}
public void setAccount(final String account) {
this.account = account;
}
public String getNetwork() {
return network;
}
public void setNetwork(final String network) {
this.network = network;
}
public long getPercent() {
return percent;
}
public void setPercent(final long percent) {
this.percent = percent;
}
public String getMessageKey() {
return messageKey;
}
public void setMessageKey(final String messageKey) {
this.messageKey = messageKey;
}
}

+ 81
- 2
ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java Visa fil

@@ -3,6 +3,7 @@ package com.getbubblenow.android.repository;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.util.Log;
@@ -12,6 +13,7 @@ import com.getbubblenow.android.Application;
import com.getbubblenow.android.R;
import com.getbubblenow.android.configStore.FileConfigStore;
import com.getbubblenow.android.model.Network;
import com.getbubblenow.android.model.NetworkStatus;
import com.getbubblenow.android.model.ObservableTunnel;
import com.getbubblenow.android.model.TunnelManager;
import com.getbubblenow.android.activity.MainActivity;
@@ -83,6 +85,16 @@ public class DataRepository {
private List<String> nodes;
private int nodeIndex = 0;
private String hostname = "";
private NetworkProgressStatusInterface networkProgressStatusInterface;
private ShowNetworkProgressInterface showNetworkProgressInterface;

public void setShowNetworkProgressInterface(final ShowNetworkProgressInterface showNetworkProgressInterface) {
this.showNetworkProgressInterface = showNetworkProgressInterface;
}

public void setNetworkProgressStateInterface(final NetworkProgressStatusInterface networkProgressStatusInterface) {
this.networkProgressStatusInterface = networkProgressStatusInterface;
}

private DataRepository(Context context, String url) {
BASE_URL = url;
@@ -141,6 +153,7 @@ public class DataRepository {
.subscribe(networks -> {
for (Network network : networks) {
if (network.getState().equals(RUNNING)) {
showNetworkProgressInterface.closeProgress();
ApiConstants.BASE_URL = network.getName() + "." + network.getDomainName();
BASE_URL = ApiConstants.BASE_URL;
hostname = BASE_URL;
@@ -148,8 +161,65 @@ public class DataRepository {
buildClientService(BASE_URL_PREFIX + BASE_URL + BASE_URL_SUFFIX);
setUserURL(context, BASE_URL_PREFIX + BASE_URL + BASE_URL_SUFFIX);
login(username,password,context,liveData);
break;
}
else {
showNetworkProgressInterface.showProgress();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override public void run() {
final HashMap<String, String> header = new HashMap<>();
header.put(ApiConstants.AUTHORIZATION_HEADER, token);
Disposable getNetworkStateDisposable = clientApi.getNetworkState(username,network.getUuid(),header)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(networkStatus -> {
if(networkProgressStatusInterface!=null) {
int indexNetworkState = 0;
for(int i = 0; i<networkStatus.size(); i++){
if(networkStatus.get(i).getNetwork().equals(network.getUuid())){
indexNetworkState = i;
break;
}
else {
//TODO Handle when don't have equals network
}
}
if(networkStatus.get(indexNetworkState).getPercent()==100){
Disposable getNodeBaseURIDisposable = clientApi.getNodeBaseURI(header)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(networks -> {
for (Network network : networks) {
if (network.getState().equals(RUNNING)) {
showNetworkProgressInterface.closeProgress();
ApiConstants.BASE_URL = network.getName() + "." + network.getDomainName();
BASE_URL = ApiConstants.BASE_URL;
hostname = BASE_URL;
setHostName(context, BASE_URL);
buildClientService(BASE_URL_PREFIX + BASE_URL + BASE_URL_SUFFIX);
setUserURL(context, BASE_URL_PREFIX + BASE_URL + BASE_URL_SUFFIX);
handler.removeCallbacks(this);
login(username, password, context, liveData);
}
}
},throwable -> {
setErrorMessage(throwable,liveData);
});
compositeDisposable.add(getNodeBaseURIDisposable);
}
else {
networkProgressStatusInterface.setProgress(networkStatus.get(indexNetworkState).getPercent(),networkStatus.get(indexNetworkState).getMessageKey());
}
}
},throwable -> {
setErrorMessage(throwable,liveData);
});
compositeDisposable.add(getNetworkStateDisposable);
handler.postDelayed(this,5000);
}
},5000);
}

}
}, throwable -> {
Log.d("ERR", "getNodeBaseURI");
@@ -221,6 +291,7 @@ public class DataRepository {
//TODO Add errbit for this case
if (UserStore.getInstance(context).getDeviceID().equals(item.getUuid())) {
// UserStore.getInstance(context).setToken(token);
Log.i("INF","SUCCESS NULL");
setMutableLiveData(StatusResource.success(null));
hasDevice = true;
break;
@@ -293,7 +364,7 @@ public class DataRepository {
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(device -> {
UserStore.getInstance(context).setDevice(device.getName(), device.getUuid());
deviceID = device.getUuid();
getCertificate(context).observe((LifecycleOwner) context, new Observer<StatusResource<byte[]>>() {
@Override public void onChanged(final StatusResource<byte[]> statusResource) {
switch (statusResource.status){
@@ -673,4 +744,12 @@ public class DataRepository {
// tunnels.add(tunnelManager.getLastUsedTunnel());
// UtilKt.deleteTunnel(tunnels);
}
public interface NetworkProgressStatusInterface {
void setProgress(long percent, String progressTitle);
}

public interface ShowNetworkProgressInterface {
void showProgress();
void closeProgress();
}
}

+ 12
- 0
ui/src/main/java/com/getbubblenow/android/viewmodel/BaseViewModel.java Visa fil

@@ -0,0 +1,12 @@
package com.getbubblenow.android.viewmodel;

import com.getbubblenow.android.repository.DataRepository;
import com.getbubblenow.android.repository.DataRepository.ShowNetworkProgressInterface;

import androidx.lifecycle.ViewModel;

public class BaseViewModel extends ViewModel {
public void setShowNetworkProgressInterface(final ShowNetworkProgressInterface showNetworkProgressInterface) {
DataRepository.getRepositoryInstance().setShowNetworkProgressInterface(showNetworkProgressInterface);
}
}

+ 12
- 0
ui/src/main/java/com/getbubblenow/android/viewmodel/ProgressLoadingViewModel.java Visa fil

@@ -0,0 +1,12 @@
package com.getbubblenow.android.viewmodel;

import com.getbubblenow.android.repository.DataRepository;
import com.getbubblenow.android.repository.DataRepository.NetworkProgressStatusInterface;

import androidx.lifecycle.ViewModel;

public class ProgressLoadingViewModel extends ViewModel {
public void setNetworkProgressStateInterface(final NetworkProgressStatusInterface networkProgressStatusInterface) {
DataRepository.getRepositoryInstance().setNetworkProgressStateInterface(networkProgressStatusInterface);
}
}

+ 29
- 0
ui/src/main/res/layout/progress_loading_dialog.xml Visa fil

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@android:color/transparent">

<ProgressBar
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:id="@+id/network_progress"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="200dp"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/progress_title"
android:paddingTop="20dp"
android:textColor="@color/white"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>

</LinearLayout>

Laddar…
Avbryt
Spara