Skip to content

Commit

Permalink
Merge pull request #609 from ooni/issues/1709/progress-fragment
Browse files Browse the repository at this point in the history
Chore: Update `ProgressFragment` to `ViewBinding`
  • Loading branch information
aanorbel authored Sep 12, 2023
2 parents f55143a + d47ce05 commit 15cbb86
Showing 1 changed file with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,35 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.activity.RunningActivity;
import org.openobservatory.ooniprobe.common.Application;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.TestProgressRepository;
import org.openobservatory.ooniprobe.common.service.RunTestService;
import org.openobservatory.ooniprobe.databinding.FragmentProgressBinding;
import org.openobservatory.ooniprobe.receiver.TestRunBroadRequestReceiver;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Monitors and displays progress of {@link RunTestService}.
*/
public class ProgressFragment extends Fragment {
private TestRunBroadRequestReceiver receiver;

private FragmentProgressBinding biding;

@Inject
PreferenceManager preferenceManager;
@Inject
TestProgressRepository testProgressRepository;
@BindView(R.id.progress_layout)
FrameLayout progress_layout;
@BindView(R.id.progress)
ProgressBar progress;
@BindView(R.id.running)
TextView running;
@BindView(R.id.name)
TextView name;

public ProgressFragment() {
// Required empty public constructor
Expand All @@ -62,24 +47,18 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_progress, container, false);
ButterKnife.bind(this, v);
biding = FragmentProgressBinding.inflate(inflater, container, false);
((Application) getActivity().getApplication()).getFragmentComponent().inject(this);
v.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Intent intent = new Intent(getContext(), RunningActivity.class);
ActivityCompat.startActivity(getActivity(), intent, null);
}
return true;
}
biding.getRoot().setOnClickListener(v -> {
Intent intent = new Intent(getContext(), RunningActivity.class);
ActivityCompat.startActivity(getActivity(), intent, null);
});
testProgressRepository.getProgress().observe(getViewLifecycleOwner(),progressValue -> {
if (progressValue!=null) {
progress.setProgress(progressValue);
biding.progress.setProgress(progressValue);
}
});
return v;
return biding.getRoot();
}

@Override
Expand All @@ -97,10 +76,10 @@ public void bindTestService() {
if (activity!=null && ((Application)activity.getApplication()).isTestRunning()) {
Intent intent = new Intent(getActivity(), RunTestService.class);
getActivity().bindService(intent, receiver, Context.BIND_AUTO_CREATE);
progress_layout.setVisibility(View.VISIBLE);
biding.progressLayout.setVisibility(View.VISIBLE);
}
else
progress_layout.setVisibility(View.GONE);
biding.progressLayout.setVisibility(View.GONE);
}

private void updateUI(RunTestService service){
Expand All @@ -109,15 +88,15 @@ private void updateUI(RunTestService service){

Integer progressLevel = testProgressRepository.getProgress().getValue();
if (progressLevel != null) {
progress.setProgress(progressLevel);
biding.progress.setProgress(progressLevel);
} else {
progress.setIndeterminate(true);
biding.progress.setIndeterminate(true);
}
if (service != null && service.task != null){
if (service.task.currentSuite != null)
progress.setMax(service.task.getMax(preferenceManager));
biding.progress.setMax(service.task.getMax(preferenceManager));
if (service.task.currentTest != null)
name.setText(getString(service.task.currentTest.getLabelResId()));
biding.name.setText(getString(service.task.currentTest.getLabelResId()));
}
}
}
Expand Down Expand Up @@ -146,15 +125,15 @@ public void onStart(RunTestService service) {

@Override
public void onRun(String value) {
name.setText(value);
biding.name.setText(value);
}

@Override
public void onProgress(int state, double eta) {
if (progress.isIndeterminate())
if (biding.progress.isIndeterminate())
updateUI(receiver.service);
progress.setIndeterminate(false);
progress.setProgress(state);
biding.progress.setIndeterminate(false);
biding.progress.setProgress(state);
}

@Override
Expand All @@ -169,17 +148,17 @@ public void onError(String value) {

@Override
public void onUrl() {
progress.setIndeterminate(false);
biding.progress.setIndeterminate(false);
}

@Override
public void onInterrupt() {
running.setText(getString(R.string.Dashboard_Running_Stopping_Title));
biding.running.setText(getString(R.string.Dashboard_Running_Stopping_Title));
}

@Override
public void onEnd(Context context) {
progress_layout.setVisibility(View.GONE);
biding.progressLayout.setVisibility(View.GONE);
}
}
}

0 comments on commit 15cbb86

Please sign in to comment.