diff --git a/ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java b/ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java index 4889902..02fb62b 100644 --- a/ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java +++ b/ui/src/main/java/com/getbubblenow/android/activity/BaseActivityBubble.java @@ -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; + } + } } diff --git a/ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java b/ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java index 293e4bd..a2df179 100644 --- a/ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java +++ b/ui/src/main/java/com/getbubblenow/android/api/ApiConstants.java @@ -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"; } diff --git a/ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java b/ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java index d9f4fd9..ed56b30 100644 --- a/ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java +++ b/ui/src/main/java/com/getbubblenow/android/api/network/ClientApi.java @@ -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> getNodeBaseURI(@HeaderMap HashMap header); + + @GET(ApiConstants.NETWORK_STATUS) + Single> getNetworkState(@Path("userId") String userId , @Path("networkId") String networkId , @HeaderMap HashMap header); } diff --git a/ui/src/main/java/com/getbubblenow/android/fragment/ProgressLoadingDialogFragment.java b/ui/src/main/java/com/getbubblenow/android/fragment/ProgressLoadingDialogFragment.java new file mode 100644 index 0000000..2a7779a --- /dev/null +++ b/ui/src/main/java/com/getbubblenow/android/fragment/ProgressLoadingDialogFragment.java @@ -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); + } +} diff --git a/ui/src/main/java/com/getbubblenow/android/model/NetworkStatus.java b/ui/src/main/java/com/getbubblenow/android/model/NetworkStatus.java new file mode 100644 index 0000000..049e2c1 --- /dev/null +++ b/ui/src/main/java/com/getbubblenow/android/model/NetworkStatus.java @@ -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; + } +} diff --git a/ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java b/ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java index db6c3ac..8dc1731 100644 --- a/ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java +++ b/ui/src/main/java/com/getbubblenow/android/repository/DataRepository.java @@ -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 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 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 { + 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>() { @Override public void onChanged(final StatusResource 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(); + } } diff --git a/ui/src/main/java/com/getbubblenow/android/viewmodel/BaseViewModel.java b/ui/src/main/java/com/getbubblenow/android/viewmodel/BaseViewModel.java new file mode 100644 index 0000000..a965b89 --- /dev/null +++ b/ui/src/main/java/com/getbubblenow/android/viewmodel/BaseViewModel.java @@ -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); + } +} diff --git a/ui/src/main/java/com/getbubblenow/android/viewmodel/ProgressLoadingViewModel.java b/ui/src/main/java/com/getbubblenow/android/viewmodel/ProgressLoadingViewModel.java new file mode 100644 index 0000000..879a046 --- /dev/null +++ b/ui/src/main/java/com/getbubblenow/android/viewmodel/ProgressLoadingViewModel.java @@ -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); + } +} diff --git a/ui/src/main/res/layout/progress_loading_dialog.xml b/ui/src/main/res/layout/progress_loading_dialog.xml new file mode 100644 index 0000000..56cda35 --- /dev/null +++ b/ui/src/main/res/layout/progress_loading_dialog.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file