Allow to navigate through viewpager

This commit is contained in:
Thomas 2020-12-15 16:31:06 +01:00
parent 3bf596f1cf
commit efe9a88209
14 changed files with 147 additions and 117 deletions

View File

@ -3,7 +3,7 @@
<color name="colorPrimary">#005e8b</color>
<color name="colorPrimaryDark">#3d2b43</color>
<color name="colorAccent">#007bff</color>
<color name="percent">#77005e8b</color>
<color name="colorGreen">#6fc384</color>
<color name="colorLightRed">#e46772</color>
<color name="colorLightYellow">#ffdb66</color>

View File

@ -3,6 +3,7 @@
<color name="colorPrimary">#684971</color>
<color name="colorPrimaryDark">#3d2b43</color>
<color name="colorAccent">#3d2b43</color>
<color name="percent">#77684971</color>
<color name="colorGreen">#6fc384</color>
<color name="colorLightRed">#e46772</color>

View File

@ -51,8 +51,6 @@ public class CheckAppActivity extends AppCompatActivity implements NetworkListen
private static final Pattern fdroidRegex = Pattern.compile("https?://f-droid\\.org/([\\w-]+/)?packages/([\\w.-]+)");
private static final Pattern googleRegex = Pattern.compile("https?://play\\.google\\.com/store/apps/details\\?id=([\\w.-]+)");
private String app_id;
ArrayList<Updatable> fragments;
AppCheckActivityBinding binding;
TrackerListAdapter.OnTrackerClickListener onTrackerClickListener = id -> {
@ -65,6 +63,7 @@ public class CheckAppActivity extends AppCompatActivity implements NetworkListen
.addToBackStack(null)
.commit();
};
private String app_id;
private TrackerListAdapter.OnTrackerClickListener trackerClickListener;
@Override

View File

@ -72,8 +72,6 @@ public class MainActivity extends AppCompatActivity {
private Menu toolbarMenu;
private String packageName;
private MainBinding binding;
private ApplicationListAdapter.OnAppClickListener onAppClickListener;
private String previousQuery = "";
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
int itemId = item.getItemId();
@ -84,6 +82,9 @@ public class MainActivity extends AppCompatActivity {
}
return true;
};
private ApplicationListAdapter.OnAppClickListener onAppClickListener;
private TrackerListAdapter.OnTrackerClickListener onTrackerClickListener;
private String previousQuery = "";
private HomeFragment home;
@Override
@ -142,13 +143,19 @@ public class MainActivity extends AppCompatActivity {
binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
MenuItem item = binding.navView.getMenu().getItem(position);
binding.navView.setSelectedItemId(item.getItemId());
if (binding.fragmentContainer.getVisibility() == View.VISIBLE) {
while (fragments.size() > 0) {
getSupportFragmentManager().popBackStack();
fragments.remove(fragments.size() - 1);
}
binding.fragmentContainer.setVisibility(View.GONE);
}
}
@Override
@ -157,18 +164,20 @@ public class MainActivity extends AppCompatActivity {
}
});
TrackerListAdapter.OnTrackerClickListener onTrackerClickListener = id -> {
onTrackerClickListener = id -> {
TrackerFragment tracker = TrackerFragment.newInstance(id);
tracker.setOnAppClickListener(onAppClickListener);
fragments.add(tracker);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
binding.fragmentContainer.setVisibility(View.VISIBLE);
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_left)
.replace(R.id.fragment_container, tracker)
.addToBackStack(null)
.commit();
};
onAppClickListener = (vm) -> {
try {
@ -256,8 +265,10 @@ public class MainActivity extends AppCompatActivity {
}
});
MenuItem settingsMenuItem = menu.findItem(R.id.action_settings);
if (fragments.size() > 0) {
Updatable fragment = fragments.get(fragments.size() - 1);
settingsMenuItem.setVisible(fragment instanceof ReportFragment);
}
return true;
}
@ -331,9 +342,12 @@ public class MainActivity extends AppCompatActivity {
@NonNull
@Override
public Fragment getItem(final int position) {
//noinspection SwitchStatementWithTooFewBranches
switch (position) {
case 1:
return new MyTrackersFragment();
MyTrackersFragment myTrackersFragment = new MyTrackersFragment();
myTrackersFragment.setOnTrackerClickListener(onTrackerClickListener);
return myTrackersFragment;
default:
return home;
}

View File

@ -5,7 +5,9 @@ import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@ -16,14 +18,8 @@ import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -289,15 +285,16 @@ public class Utils {
}
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (Comparator<Object>) (o1, o2) -> ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue()));
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
/**
* Converts dp to pixel
*
* @param dp float - the value in dp to convert
* @param context Context
* @return float - the converted value in pixel
*/
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
}

View File

@ -19,6 +19,7 @@ package org.eu.exodus_privacy.exodusprivacy.adapters;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
@ -26,9 +27,7 @@ import androidx.recyclerview.widget.RecyclerView;
import org.eu.exodus_privacy.exodusprivacy.R;
import org.eu.exodus_privacy.exodusprivacy.databinding.MyTrackerItemBinding;
import org.eu.exodus_privacy.exodusprivacy.objects.MyTracker;
import org.eu.exodus_privacy.exodusprivacy.objects.Tracker;
import java.util.ArrayList;
import java.util.List;
@ -36,13 +35,13 @@ public class MyTrackersListAdapter extends RecyclerView.Adapter<MyTrackersListAd
private final TrackerClickListener trackerClickListener;
private final List<MyTracker> myTrackers;
private List<Tracker> trackersList;
private final int max;
private int viewWidth = 0;
public MyTrackersListAdapter(List<MyTracker> mTrackers, TrackerClickListener listener) {
public MyTrackersListAdapter(List<MyTracker> mTrackers, TrackerClickListener listener, int maxValue) {
myTrackers = mTrackers;
setTrackers(mTrackers);
trackerClickListener = listener;
max = maxValue;
}
@NonNull
@ -54,35 +53,33 @@ public class MyTrackersListAdapter extends RecyclerView.Adapter<MyTrackersListAd
@Override
public void onBindViewHolder(@NonNull MyTrackersListAdapter.TrackerListViewHolder holder, int position) {
Tracker tracker = trackersList.get(position);
if (tracker != null) {
holder.viewDataBinding.trackerName.setText(tracker.name);
holder.viewDataBinding.trackerCount.setText(String.valueOf(countOccurences(tracker.codeSignature)));
holder.viewDataBinding.getRoot().setOnClickListener(v -> trackerClickListener.onTrackerClick(tracker.id));
MyTracker myTracker = myTrackers.get(position);
if (myTrackers != null) {
holder.viewDataBinding.trackerName.setText(myTracker.tracker.name);
holder.viewDataBinding.trackerCount.setText(String.valueOf(myTracker.number));
holder.viewDataBinding.getRoot().setOnClickListener(v -> {
trackerClickListener.onTrackerClick(myTracker.tracker.id);
});
float percent = (float) myTracker.number / max;
holder.viewDataBinding.percent.getLayoutParams().width = (int) (viewWidth * percent);
holder.viewDataBinding.percentVal.setText(String.format("%s %%", myTracker.number * 100 / myTrackers.size()));
holder.viewDataBinding.percent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
holder.viewDataBinding.percent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
if (viewWidth == 0) {
viewWidth = holder.viewDataBinding.percent.getWidth();
notifyDataSetChanged();
}
}
});
} else
holder.viewDataBinding.trackerName.setText(R.string.no_trackers);
}
@Override
public int getItemCount() {
return trackersList.size();
}
public void setTrackers(List<MyTracker> myTrackers) {
trackersList = new ArrayList<>();
if (myTrackers != null) {
for (MyTracker myTracker : myTrackers) {
trackersList.add(myTracker.tracker);
}
}
}
private int countOccurences(String signature) {
if (myTrackers == null) return 0;
for (MyTracker myTracker : myTrackers) {
if (myTracker.signature.compareTo(signature) == 0) return myTracker.number;
}
return 0;
return myTrackers.size();
}
public interface TrackerClickListener {

View File

@ -10,7 +10,6 @@ import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.eu.exodus_privacy.exodusprivacy.R;
import org.eu.exodus_privacy.exodusprivacy.adapters.ApplicationListAdapter;
@ -22,7 +21,7 @@ import java.util.List;
public class AppListFragment extends Fragment {
private static int firstVisiblePosition = 0;
private ApplistBinding applistBinding;
private List<ApplicationViewModel> applications;
private ApplicationListAdapter adapter;
@ -43,18 +42,6 @@ public class AppListFragment extends Fragment {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
applistBinding.appList.setLayoutManager(linearLayoutManager);
applistBinding.appList.setVerticalScrollBarEnabled(scrollbarEnabled);
applistBinding.appList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
}
});
adapter = new ApplicationListAdapter(onAppClickListener);
adapter.displayAppList(applications);
adapter.filter(filterType, filterObject);
@ -105,9 +92,6 @@ public class AppListFragment extends Fragment {
return adapter.getDisplayedApps();
}
public void scrollTo() {
applistBinding.appList.scrollToPosition(firstVisiblePosition);
}
public enum Type {
NAME,

View File

@ -114,7 +114,6 @@ public class HomeFragment extends Fragment implements ComputeAppList.Listener, U
@Override
public void onResume() {
super.onResume();
appListFragment.scrollTo();
}

View File

@ -35,6 +35,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import org.eu.exodus_privacy.exodusprivacy.R;
import org.eu.exodus_privacy.exodusprivacy.adapters.MyTrackersListAdapter;
import org.eu.exodus_privacy.exodusprivacy.adapters.TrackerListAdapter;
import org.eu.exodus_privacy.exodusprivacy.databinding.MyTrackersBinding;
import org.eu.exodus_privacy.exodusprivacy.manager.DatabaseManager;
import org.eu.exodus_privacy.exodusprivacy.objects.MyTracker;
@ -50,6 +51,8 @@ public class MyTrackersFragment extends Fragment implements MyTrackersListAdapte
private Context context;
private MyTrackersBinding trackerBinding;
private TrackerListAdapter.OnTrackerClickListener onTrackerClickListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -75,6 +78,7 @@ public class MyTrackersFragment extends Fragment implements MyTrackersListAdapte
List<PackageInfo> packageInstalled = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<MyTracker> myTrackers = new ArrayList<>();
List<String> added = new ArrayList<>();
int maxValue = 0;
for (PackageInfo pkgInfo : packageInstalled) {
Report report;
if (pkgInfo.versionName != null)
@ -102,10 +106,15 @@ public class MyTrackersFragment extends Fragment implements MyTrackersListAdapte
}
}
}
for (MyTracker myTracker : myTrackers) {
if (myTracker.number > maxValue)
maxValue = myTracker.number;
}
Handler mainHandler = new Handler(Looper.getMainLooper());
int finalMaxValue = maxValue;
Runnable myRunnable = () -> {
Collections.sort(myTrackers, (obj1, obj2) -> Integer.compare(obj2.number, obj1.number));
MyTrackersListAdapter myTrackersListAdapter = new MyTrackersListAdapter(myTrackers, MyTrackersFragment.this);
MyTrackersListAdapter myTrackersListAdapter = new MyTrackersListAdapter(myTrackers, MyTrackersFragment.this, finalMaxValue);
trackerBinding.trackers.setAdapter(myTrackersListAdapter);
trackerBinding.trackers.setLayoutManager(new LinearLayoutManager(context));
trackerBinding.trackers.setVisibility(View.VISIBLE);
@ -132,9 +141,12 @@ public class MyTrackersFragment extends Fragment implements MyTrackersListAdapte
menu.findItem(R.id.action_filter_options).setVisible(false);
}
public void setOnTrackerClickListener(TrackerListAdapter.OnTrackerClickListener listener) {
onTrackerClickListener = listener;
}
@Override
public void onTrackerClick(long trackerId) {
onTrackerClickListener.onTrackerClick(trackerId);
}
}

View File

@ -246,7 +246,6 @@ public class NetworkManager {
}
private void getApplications(Message mes) {
mes.listener.onProgress(R.string.get_reports_connection, 0, 0);
URL url;

View File

@ -1,6 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:width="64dp"
android:height="64dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/percent" />
<stroke
android:width="2dp"
android:color="@color/colorPrimary" />
<corners android:radius="5dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>

View File

@ -1,25 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data />
<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1">
<View
android:id="@+id/percent"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:background="@drawable/percent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/details"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/percent_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
app:layout_constraintBottom_toBottomOf="@+id/percent"
app:layout_constraintStart_toStartOf="@+id/percent"
app:layout_constraintTop_toTopOf="@+id/percent" />
<TextView
android:id="@+id/tracker_name"
android:layout_width="0dp"
@ -27,29 +40,30 @@
android:textColor="@color/textColorDark"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/percent"
app:layout_constraintEnd_toStartOf="@+id/tracker_count"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/percent" />
<TextView
android:id="@+id/tracker_count"
android:layout_width="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/percent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tracker_name"
app:layout_constraintTop_toTopOf="@id/tracker_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/details"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="10dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/ic_baseline_navigate_next_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:contentDescription="@string/list_of_apps" />
</LinearLayout>
</layout>

View File

@ -64,6 +64,7 @@
<string name="app_not_analyzed_title">This app has not been analyzed!</string>
<string name="title_apps">My apps</string>
<string name="title_trackers">Trackers</string>
<string name="list_of_apps">See the list of apps</string>
</resources>