diff --git a/app/src/main/java/org/fdroid/fdroid/IndexV1Updater.java b/app/src/main/java/org/fdroid/fdroid/IndexV1Updater.java index c53b52f3e..82a00247f 100644 --- a/app/src/main/java/org/fdroid/fdroid/IndexV1Updater.java +++ b/app/src/main/java/org/fdroid/fdroid/IndexV1Updater.java @@ -65,9 +65,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.TreeSet; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -295,7 +295,7 @@ public class IndexV1Updater extends IndexUpdater { repo.description = getStringRepoValue(repoMap, "description"); // ensure the canonical URL is included in the "mirrors" list as the first entry - TreeSet mirrors = new TreeSet<>(); + LinkedHashSet mirrors = new LinkedHashSet<>(); mirrors.add(repo.address); mirrors.addAll(getStringListRepoValue(repoMap, "mirrors")); repo.mirrors = mirrors.toArray(new String[mirrors.size()]); diff --git a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java index 07df7502d..4444c88f3 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -97,6 +97,7 @@ public class DBHelper extends SQLiteOpenHelper { + RepoTable.Cols.ICON + " string, " + RepoTable.Cols.MIRRORS + " string, " + RepoTable.Cols.USER_MIRRORS + " string, " + + RepoTable.Cols.DISABLED_MIRRORS + " string, " + RepoTable.Cols.PUSH_REQUESTS + " integer not null default " + Repo.PUSH_REQUEST_IGNORE + ");"; @@ -223,7 +224,7 @@ public class DBHelper extends SQLiteOpenHelper { + "primary key(" + ApkAntiFeatureJoinTable.Cols.APK_ID + ", " + ApkAntiFeatureJoinTable.Cols.ANTI_FEATURE_ID + ") " + " );"; - protected static final int DB_VERSION = 79; + protected static final int DB_VERSION = 80; private final Context context; @@ -448,6 +449,17 @@ public class DBHelper extends SQLiteOpenHelper { addLiberapayID(db, oldVersion); addUserMirrorsFields(db, oldVersion); removeNotNullFromVersionName(db, oldVersion); + addDisabledMirrorsFields(db, oldVersion); + } + + private void addDisabledMirrorsFields(SQLiteDatabase db, int oldVersion) { + if (oldVersion >= 80) { + return; + } + if (!columnExists(db, RepoTable.NAME, RepoTable.Cols.DISABLED_MIRRORS)) { + Utils.debugLog(TAG, "Adding " + RepoTable.Cols.DISABLED_MIRRORS + " field to " + RepoTable.NAME + " table in db."); + db.execSQL("alter table " + RepoTable.NAME + " add column " + RepoTable.Cols.DISABLED_MIRRORS + " string;"); + } } private void removeNotNullFromVersionName(SQLiteDatabase db, int oldVersion) { diff --git a/app/src/main/java/org/fdroid/fdroid/data/Repo.java b/app/src/main/java/org/fdroid/fdroid/data/Repo.java index 2bebb1854..fa92350ca 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/Repo.java +++ b/app/src/main/java/org/fdroid/fdroid/data/Repo.java @@ -86,6 +86,9 @@ public class Repo extends ValueObject { @JsonIgnore public int pushRequests = PUSH_REQUEST_IGNORE; + /** + * The canonical URL of the repo. + */ public String address; public String name; public String description; @@ -125,8 +128,15 @@ public class Repo extends ValueObject { /** * Mirrors added by the user, either by UI input or by attaching removeable storage */ + @JsonIgnore public String[] userMirrors; + /** + * Mirrors that have been manually disabled by the user. + */ + @JsonIgnore + public String[] disabledMirrors; + public Repo() { } @@ -193,6 +203,9 @@ public class Repo extends ValueObject { case Cols.USER_MIRRORS: userMirrors = Utils.parseCommaSeparatedString(cursor.getString(i)); break; + case Cols.DISABLED_MIRRORS: + disabledMirrors = Utils.parseCommaSeparatedString(cursor.getString(i)); + break; case Cols.PUSH_REQUESTS: pushRequests = cursor.getInt(i); break; @@ -334,6 +347,10 @@ public class Repo extends ValueObject { userMirrors = Utils.parseCommaSeparatedString(values.getAsString(Cols.USER_MIRRORS)); } + if (values.containsKey(Cols.DISABLED_MIRRORS)) { + disabledMirrors = Utils.parseCommaSeparatedString(values.getAsString(Cols.DISABLED_MIRRORS)); + } + if (values.containsKey(Cols.PUSH_REQUESTS)) { pushRequests = toInt(values.getAsInteger(Cols.PUSH_REQUESTS)); } @@ -345,8 +362,9 @@ public class Repo extends ValueObject { * mirror list. */ public boolean hasMirrors() { - return (mirrors != null && mirrors.length > 1) - || (userMirrors != null && userMirrors.length > 0); + List mirrorList = getMirrorList(); + int size = mirrorList.size(); + return size > 1 || (size == 1 && !mirrorList.contains(address)); } /** @@ -361,6 +379,9 @@ public class Repo extends ValueObject { allMirrors.addAll(Arrays.asList(mirrors)); } allMirrors.add(address); + if (disabledMirrors != null) { + allMirrors.removeAll(Arrays.asList(disabledMirrors)); + } return new ArrayList<>(allMirrors); } @@ -382,9 +403,11 @@ public class Repo extends ValueObject { /** * Get a random mirror URL from the list of mirrors for this repo. It will * remove the URL in {@code mirrorToSkip} from consideration before choosing - * which mirror to return. + * which mirror to return. {@link #getMirrorList()} returns a list of all + * known mirrors minus the mirrors that have been disabled by the + * user preference, e.g. {@link #disabledMirrors}. *

- * The mirror logic assumes that it has a mirrors list with at least once + * The mirror logic assumes that it has a mirrors list with at least one * valid entry in it. In the index format as defined by {@code fdroid update}, * there is always at least one valid URL: the canonical URL. That also means * if there is only one item in the mirrors list, there are no other URLs to try. @@ -394,6 +417,8 @@ public class Repo extends ValueObject { * update. That makes it possible to do the first index update via SD Card * or USB OTG drive. * + * @see #getMirrorList() + * @see #disabledMirrors * @see FDroidApp#resetMirrorVars() * @see FDroidApp#switchUrlToNewMirror(String, Repo) * @see FDroidApp#getTimeout() @@ -403,7 +428,7 @@ public class Repo extends ValueObject { mirrorToSkip = address; } List shuffledMirrors = getMirrorList(); - if (shuffledMirrors.size() > 1) { + if (shuffledMirrors.size() > 0) { Collections.shuffle(shuffledMirrors); for (String m : shuffledMirrors) { // Return a non default, and not last used mirror @@ -419,6 +444,6 @@ public class Repo extends ValueObject { } } } - return null; // In case we are out of mirrors. + return address; // In case we are out of mirrors. } } diff --git a/app/src/main/java/org/fdroid/fdroid/data/Schema.java b/app/src/main/java/org/fdroid/fdroid/data/Schema.java index 3642ebcd9..bdb8c01d8 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/Schema.java +++ b/app/src/main/java/org/fdroid/fdroid/data/Schema.java @@ -363,12 +363,13 @@ public interface Schema { String ICON = "icon"; String MIRRORS = "mirrors"; String USER_MIRRORS = "userMirrors"; + String DISABLED_MIRRORS = "disabledMirrors"; String PUSH_REQUESTS = "pushRequests"; String[] ALL = { _ID, ADDRESS, NAME, DESCRIPTION, IN_USE, PRIORITY, SIGNING_CERT, FINGERPRINT, MAX_AGE, LAST_UPDATED, LAST_ETAG, VERSION, IS_SWAP, - USERNAME, PASSWORD, TIMESTAMP, ICON, MIRRORS, USER_MIRRORS, PUSH_REQUESTS, + USERNAME, PASSWORD, TIMESTAMP, ICON, MIRRORS, USER_MIRRORS, DISABLED_MIRRORS, PUSH_REQUESTS, }; } } diff --git a/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java b/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java index 7d2fbac08..55846ac36 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/RepoDetailsActivity.java @@ -13,17 +13,23 @@ import android.nfc.NfcAdapter; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; +import android.support.annotation.NonNull; import android.support.v4.app.NavUtils; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.text.format.DateUtils; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.widget.Button; +import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; @@ -38,6 +44,8 @@ import org.fdroid.fdroid.data.Repo; import org.fdroid.fdroid.data.RepoProvider; import org.fdroid.fdroid.data.Schema.RepoTable; +import java.util.Arrays; +import java.util.HashSet; import java.util.Locale; public class RepoDetailsActivity extends AppCompatActivity { @@ -76,6 +84,8 @@ public class RepoDetailsActivity extends AppCompatActivity { private View repoView; private String shareUrl; + private MirrorAdapter adapterToNotify; + /** * Help function to make switching between two view states easier. * Perhaps there is a better way to do this. I recall that using Adobe @@ -103,18 +113,20 @@ public class RepoDetailsActivity extends AppCompatActivity { repoView = findViewById(R.id.repo_view); repoId = getIntent().getLongExtra(ARG_REPO_ID, 0); - final String[] projection = { - RepoTable.Cols.NAME, - RepoTable.Cols.ADDRESS, - RepoTable.Cols.FINGERPRINT, - RepoTable.Cols.MIRRORS, - RepoTable.Cols.USER_MIRRORS, - }; - repo = RepoProvider.Helper.findById(this, repoId, projection); + repo = RepoProvider.Helper.findById(this, repoId); TextView inputUrl = findViewById(R.id.input_repo_url); inputUrl.setText(repo.address); + RecyclerView officialMirrorListView = findViewById(R.id.official_mirror_list); + officialMirrorListView.setLayoutManager(new LinearLayoutManager(this)); + adapterToNotify = new MirrorAdapter(repo, repo.mirrors); + officialMirrorListView.setAdapter(adapterToNotify); + + RecyclerView userMirrorListView = findViewById(R.id.user_mirror_list); + userMirrorListView.setLayoutManager(new LinearLayoutManager(this)); + userMirrorListView.setAdapter(new MirrorAdapter(repo, repo.userMirrors)); + if (repo.address.startsWith("content://")) { // no need to show a QR Code, it is not shareable return; @@ -324,18 +336,36 @@ public class RepoDetailsActivity extends AppCompatActivity { } private void updateRepoView() { + TextView officialMirrorsLabel = repoView.findViewById(R.id.label_official_mirrors); + RecyclerView officialMirrorList = repoView.findViewById(R.id.official_mirror_list); + if ((repo.mirrors != null && repo.mirrors.length > 1) + || (repo.userMirrors != null && repo.userMirrors.length > 0)) { + // don't show this if there is only the canonical URL available, and no other mirrors + officialMirrorsLabel.setVisibility(View.VISIBLE); + officialMirrorList.setVisibility(View.VISIBLE); + } else { + officialMirrorsLabel.setVisibility(View.GONE); + officialMirrorList.setVisibility(View.GONE); + } + + TextView userMirrorsLabel = repoView.findViewById(R.id.label_user_mirrors); + RecyclerView userMirrorList = repoView.findViewById(R.id.user_mirror_list); + if (repo.userMirrors != null && repo.userMirrors.length > 0) { + userMirrorsLabel.setVisibility(View.VISIBLE); + userMirrorList.setVisibility(View.VISIBLE); + } else { + userMirrorsLabel.setVisibility(View.GONE); + userMirrorList.setVisibility(View.GONE); + } + if (repo.hasBeenUpdated()) { updateViewForExistingRepo(repoView); } else { - updateViewForNewRepo(repoView); + setMultipleViewVisibility(repoView, HIDE_IF_EXISTS, View.VISIBLE); + setMultipleViewVisibility(repoView, SHOW_IF_EXISTS, View.GONE); } } - private void updateViewForNewRepo(View repoView) { - setMultipleViewVisibility(repoView, HIDE_IF_EXISTS, View.VISIBLE); - setMultipleViewVisibility(repoView, SHOW_IF_EXISTS, View.GONE); - } - private void updateViewForExistingRepo(View repoView) { setMultipleViewVisibility(repoView, SHOW_IF_EXISTS, View.VISIBLE); setMultipleViewVisibility(repoView, HIDE_IF_EXISTS, View.GONE); @@ -344,35 +374,6 @@ public class RepoDetailsActivity extends AppCompatActivity { TextView numApps = repoView.findViewById(R.id.text_num_apps); TextView lastUpdated = repoView.findViewById(R.id.text_last_update); - if (repo.mirrors != null) { - TextView officialMirrorsLabel = repoView.findViewById(R.id.label_official_mirrors); - officialMirrorsLabel.setVisibility(View.VISIBLE); - TextView officialMirrorsText = repoView.findViewById(R.id.text_official_mirrors); - officialMirrorsText.setVisibility(View.VISIBLE); - StringBuilder builder = new StringBuilder(); - for (String url : repo.mirrors) { - builder.append("• "); - builder.append(url); - builder.append('\n'); - } - builder.setLength(Math.max(builder.length() - 1, 0)); - officialMirrorsText.setText(builder.toString()); - } - if (repo.userMirrors != null) { - TextView userMirrorsLabel = repoView.findViewById(R.id.label_user_mirrors); - userMirrorsLabel.setVisibility(View.VISIBLE); - TextView userMirrorsText = repoView.findViewById(R.id.text_user_mirrors); - userMirrorsText.setVisibility(View.VISIBLE); - StringBuilder builder = new StringBuilder(); - for (String url : repo.userMirrors) { - builder.append("• "); - builder.append(url); - builder.append('\n'); - } - builder.setLength(Math.max(builder.length() - 1, 0)); - userMirrorsText.setText(builder.toString()); - } - name.setText(repo.name); int appCount = RepoProvider.Helper.countAppsForRepo(this, repoId); @@ -464,4 +465,98 @@ public class RepoDetailsActivity extends AppCompatActivity { credentialsDialog.show(); } + + private class MirrorAdapter extends RecyclerView.Adapter { + private final Repo repo; + private final String[] mirrors; + + class MirrorViewHolder extends RecyclerView.ViewHolder { + View view; + + MirrorViewHolder(View view) { + super(view); + this.view = view; + } + } + + MirrorAdapter(Repo repo, String[] mirrors) { + this.repo = repo; + this.mirrors = mirrors; + } + + @NonNull + @Override + public MirrorAdapter.MirrorViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.repo_item, parent, false); + return new MirrorViewHolder(itemView); + } + + @Override + public void onBindViewHolder(@NonNull MirrorViewHolder holder, final int position) { + TextView repoNameTextView = holder.view.findViewById(R.id.repo_name); + repoNameTextView.setText(mirrors[position]); + + final String itemMirror = mirrors[position]; + boolean enabled = true; + if (repo.disabledMirrors != null) { + for (String disabled : repo.disabledMirrors) { + if (TextUtils.equals(itemMirror, disabled)) { + enabled = false; + break; + } + } + } + CompoundButton switchView = holder.view.findViewById(R.id.repo_switch); + switchView.setChecked(enabled); + switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + HashSet disabledMirrors; + if (repo.disabledMirrors == null) { + disabledMirrors = new HashSet<>(1); + } else { + disabledMirrors = new HashSet<>(Arrays.asList(repo.disabledMirrors)); + } + + if (isChecked) { + disabledMirrors.remove(itemMirror); + } else { + disabledMirrors.add(itemMirror); + } + + int totalMirrors = (repo.mirrors == null ? 0 : repo.mirrors.length) + + (repo.userMirrors == null ? 0 : repo.userMirrors.length); + if (disabledMirrors.size() == totalMirrors) { + // if all mirrors are disabled, re-enable canonical repo as mirror + disabledMirrors.remove(repo.address); + adapterToNotify.notifyItemChanged(0); + } + + if (disabledMirrors.size() == 0) { + repo.disabledMirrors = null; + } else { + repo.disabledMirrors = disabledMirrors.toArray(new String[disabledMirrors.size()]); + } + final ContentValues values = new ContentValues(1); + values.put(RepoTable.Cols.DISABLED_MIRRORS, + Utils.serializeCommaSeparatedString(repo.disabledMirrors)); + RepoProvider.Helper.update(RepoDetailsActivity.this, repo, values); + } + }); + + View repoUnverified = holder.view.findViewById(R.id.repo_unverified); + repoUnverified.setVisibility(View.GONE); + + View repoUnsigned = holder.view.findViewById(R.id.repo_unsigned); + repoUnsigned.setVisibility(View.GONE); + } + + @Override + public int getItemCount() { + if (mirrors == null) { + return 0; + } + return mirrors.length; + } + } } diff --git a/app/src/main/res/layout/activity_repo_details.xml b/app/src/main/res/layout/activity_repo_details.xml index 68de5e0d0..60b11ed72 100644 --- a/app/src/main/res/layout/activity_repo_details.xml +++ b/app/src/main/res/layout/activity_repo_details.xml @@ -1,6 +1,7 @@ @@ -83,10 +84,11 @@ android:text="@string/repo_official_mirrors" android:visibility="gone" /> - + - +