when receiving a repo URI with wifi info, warn user if on different wifi

A repo URI can include info such as the current wifi SSID and BSSID that
the repo is hosted on.  This is for when local repos are transmitted via
QRCode, NFC, etc.  The receiver of this URI then checks to make sure it is
on the same wifi access point, and warns the user if not.

In the future, it should do more than just warn the user, but instead give
concrete actions for the user to take, like associating to that wifi.
This commit is contained in:
Hans-Christoph Steiner 2014-01-24 11:27:32 -05:00
parent 7c67db22f3
commit 7b7da9a110
2 changed files with 30 additions and 0 deletions

View File

@ -196,5 +196,6 @@
need to re-enable this repository to install apps from it.
</string>
<string name="minsdk_or_later">%s or later</string>
<string name="not_on_same_wifi">Your device is not on the same WiFi as the local repo you just added! Try joining this network: %s</string>
</resources>

View File

@ -22,6 +22,7 @@ package org.fdroid.fdroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
@ -29,11 +30,14 @@ import android.support.v4.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.NavUtils;
import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
@ -111,6 +115,8 @@ class RepoListFragment extends ListFragment
private final int ADD_REPO = 1;
private final int UPDATE_REPOS = 2;
private WifiManager wifiManager;
public boolean hasChanged() {
return changed;
}
@ -263,6 +269,29 @@ class RepoListFragment extends ListFragment
.replace(intent.getScheme(), scheme) // downcase scheme
.replace("fdroidrepo", "http"); // proper repo address
showAddRepo(uriString, fingerprint);
// if this is a local repo, check we're on the same wifi
String uriBssid = uri.getQueryParameter("bssid");
if (!TextUtils.isEmpty(uriBssid)) {
if (uri.getPort() != 8888
&& !host.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+")) {
Log.i("ManageRepo", "URI is not local repo: " + uri);
return;
}
Activity a = getActivity();
if (wifiManager == null)
wifiManager = (WifiManager) a.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String bssid = wifiInfo.getBSSID().toLowerCase(Locale.ENGLISH);
uriBssid = Uri.decode(uriBssid).toLowerCase(Locale.ENGLISH);
if (!bssid.equals(uriBssid)) {
String msg = String.format(getString(R.string.not_on_same_wifi),
uri.getQueryParameter("ssid"));
Toast.makeText(a, msg, Toast.LENGTH_LONG).show();
}
// TODO we should help the user to the right thing here,
// instead of just showing a message!
}
}
}
}