Fixed all warnings from LocalRepoManager.java
* Removed dead code * Added some Nullable/NonNull annotations to prevent future misuses of variables. * More verbose errors when an error occurs creating directories/files.
This commit is contained in:
parent
23ed692436
commit
c6705e2cb9
@ -59,6 +59,8 @@ import java.util.Locale;
|
|||||||
|
|
||||||
public final class Utils {
|
public final class Utils {
|
||||||
|
|
||||||
|
private static final String TAG = "org.fdroid.fdroid.Utils";
|
||||||
|
|
||||||
public static final int BUFFER_SIZE = 4096;
|
public static final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
// The date format used for storing dates (e.g. lastupdated, added) in the
|
// The date format used for storing dates (e.g. lastupdated, added) in the
|
||||||
|
@ -15,6 +15,8 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -70,16 +72,13 @@ public class LocalRepoManager {
|
|||||||
private final SharedPreferences prefs;
|
private final SharedPreferences prefs;
|
||||||
private final String fdroidPackageName;
|
private final String fdroidPackageName;
|
||||||
|
|
||||||
private String ipAddressString = "UNSET";
|
|
||||||
private String uriString = "UNSET";
|
|
||||||
|
|
||||||
private static String[] WEB_ROOT_ASSET_FILES = {
|
private static String[] WEB_ROOT_ASSET_FILES = {
|
||||||
"swap-icon.png",
|
"swap-icon.png",
|
||||||
"swap-tick-done.png",
|
"swap-tick-done.png",
|
||||||
"swap-tick-not-done.png"
|
"swap-tick-not-done.png"
|
||||||
};
|
};
|
||||||
|
|
||||||
private Map<String, App> apps = new HashMap<String, App>();
|
private Map<String, App> apps = new HashMap<>();
|
||||||
|
|
||||||
public final SanitizedFile xmlIndex;
|
public final SanitizedFile xmlIndex;
|
||||||
private SanitizedFile xmlIndexJar = null;
|
private SanitizedFile xmlIndexJar = null;
|
||||||
@ -89,8 +88,10 @@ public class LocalRepoManager {
|
|||||||
public final SanitizedFile repoDir;
|
public final SanitizedFile repoDir;
|
||||||
public final SanitizedFile iconsDir;
|
public final SanitizedFile iconsDir;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private static LocalRepoManager localRepoManager;
|
private static LocalRepoManager localRepoManager;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
public static LocalRepoManager get(Context context) {
|
public static LocalRepoManager get(Context context) {
|
||||||
if (localRepoManager == null)
|
if (localRepoManager == null)
|
||||||
localRepoManager = new LocalRepoManager(context);
|
localRepoManager = new LocalRepoManager(context);
|
||||||
@ -126,10 +127,6 @@ public class LocalRepoManager {
|
|||||||
Log.e(TAG, "Unable to create icons folder: " + iconsDir);
|
Log.e(TAG, "Unable to create icons folder: " + iconsDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUriString(String uriString) {
|
|
||||||
this.uriString = uriString;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String writeFdroidApkToWebroot() {
|
private String writeFdroidApkToWebroot() {
|
||||||
ApplicationInfo appInfo;
|
ApplicationInfo appInfo;
|
||||||
String fdroidClientURL = "https://f-droid.org/FDroid.apk";
|
String fdroidClientURL = "https://f-droid.org/FDroid.apk";
|
||||||
@ -138,7 +135,7 @@ public class LocalRepoManager {
|
|||||||
appInfo = pm.getApplicationInfo(fdroidPackageName, PackageManager.GET_META_DATA);
|
appInfo = pm.getApplicationInfo(fdroidPackageName, PackageManager.GET_META_DATA);
|
||||||
SanitizedFile apkFile = SanitizedFile.knownSanitized(appInfo.publicSourceDir);
|
SanitizedFile apkFile = SanitizedFile.knownSanitized(appInfo.publicSourceDir);
|
||||||
SanitizedFile fdroidApkLink = new SanitizedFile(webRoot, "fdroid.client.apk");
|
SanitizedFile fdroidApkLink = new SanitizedFile(webRoot, "fdroid.client.apk");
|
||||||
fdroidApkLink.delete();
|
attemptToDelete(fdroidApkLink);
|
||||||
if (Utils.symlinkOrCopyFile(apkFile, fdroidApkLink))
|
if (Utils.symlinkOrCopyFile(apkFile, fdroidApkLink))
|
||||||
fdroidClientURL = "/" + fdroidApkLink.getName();
|
fdroidClientURL = "/" + fdroidApkLink.getName();
|
||||||
} catch (NameNotFoundException e) {
|
} catch (NameNotFoundException e) {
|
||||||
@ -176,10 +173,10 @@ public class LocalRepoManager {
|
|||||||
|
|
||||||
// add in /FDROID/REPO to support bad QR Scanner apps
|
// add in /FDROID/REPO to support bad QR Scanner apps
|
||||||
File fdroidCAPS = new File(fdroidDir.getParentFile(), "FDROID");
|
File fdroidCAPS = new File(fdroidDir.getParentFile(), "FDROID");
|
||||||
fdroidCAPS.mkdir();
|
attemptToMkdir(fdroidCAPS);
|
||||||
|
|
||||||
File repoCAPS = new File(fdroidCAPS, "REPO");
|
File repoCAPS = new File(fdroidCAPS, "REPO");
|
||||||
repoCAPS.mkdir();
|
attemptToMkdir(repoCAPS);
|
||||||
|
|
||||||
symlinkIndexPageElsewhere("../", fdroidCAPS);
|
symlinkIndexPageElsewhere("../", fdroidCAPS);
|
||||||
symlinkIndexPageElsewhere("../../", repoCAPS);
|
symlinkIndexPageElsewhere("../../", repoCAPS);
|
||||||
@ -190,15 +187,37 @@ public class LocalRepoManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void attemptToMkdir(@NonNull File dir) throws IOException {
|
||||||
|
if (dir.exists()) {
|
||||||
|
if (dir.isDirectory()) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw new IOException("Can't make directory " + dir + " - it is already a file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dir.mkdir()) {
|
||||||
|
throw new IOException("An error occured trying to create directory " + dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void attemptToDelete(File file) {
|
||||||
|
Utils.symlinkOrCopyFile(new SanitizedFile(new File(directory, symlinkPrefix), fileName), file);
|
||||||
|
if (!file.delete()) {
|
||||||
|
Log.e(TAG, "Could not delete \"" + file.getAbsolutePath() + "\".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void symlinkIndexPageElsewhere(String symlinkPrefix, File directory) {
|
private void symlinkIndexPageElsewhere(String symlinkPrefix, File directory) {
|
||||||
SanitizedFile index = new SanitizedFile(directory, "index.html");
|
SanitizedFile index = new SanitizedFile(directory, "index.html");
|
||||||
index.delete();
|
attemptToDelete(index);
|
||||||
Utils.symlinkOrCopyFile(new SanitizedFile(new File(symlinkPrefix), "index.html"), index);
|
Utils.symlinkOrCopyFile(new SanitizedFile(new File(directory, symlinkPrefix), "index.html"), index);
|
||||||
|
|
||||||
for(String fileName : WEB_ROOT_ASSET_FILES) {
|
for(String fileName : WEB_ROOT_ASSET_FILES) {
|
||||||
SanitizedFile file = new SanitizedFile(directory, fileName);
|
SanitizedFile file = new SanitizedFile(directory, fileName);
|
||||||
file.delete();
|
attemptToDelete(file);
|
||||||
Utils.symlinkOrCopyFile(new SanitizedFile(new File(symlinkPrefix), fileName), file);
|
Utils.symlinkOrCopyFile(new SanitizedFile(new File(directory, symlinkPrefix), fileName), file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +227,7 @@ public class LocalRepoManager {
|
|||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
deleteContents(file);
|
deleteContents(file);
|
||||||
} else {
|
} else {
|
||||||
file.delete();
|
attemptToDelete(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +238,7 @@ public class LocalRepoManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void copyApksToRepo() {
|
public void copyApksToRepo() {
|
||||||
copyApksToRepo(new ArrayList<String>(apps.keySet()));
|
copyApksToRepo(new ArrayList<>(apps.keySet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void copyApksToRepo(List<String> appsToCopy) {
|
public void copyApksToRepo(List<String> appsToCopy) {
|
||||||
@ -236,10 +255,6 @@ public class LocalRepoManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ScanListener {
|
|
||||||
public void processedApp(String packageName, int index, int total);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TargetApi(9)
|
@TargetApi(9)
|
||||||
public void addApp(Context context, String packageName) {
|
public void addApp(Context context, String packageName) {
|
||||||
App app;
|
App app;
|
||||||
@ -249,15 +264,7 @@ public class LocalRepoManager {
|
|||||||
return;
|
return;
|
||||||
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_META_DATA);
|
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_META_DATA);
|
||||||
app.icon = getIconFile(packageName, packageInfo.versionCode).getName();
|
app.icon = getIconFile(packageName, packageInfo.versionCode).getName();
|
||||||
} catch (NameNotFoundException e) {
|
} catch (NameNotFoundException | CertificateEncodingException | IOException e) {
|
||||||
Log.e(TAG, "Error adding app to local repo: " + e.getMessage());
|
|
||||||
Log.e(TAG, Log.getStackTraceString(e));
|
|
||||||
return;
|
|
||||||
} catch (CertificateEncodingException e) {
|
|
||||||
Log.e(TAG, "Error adding app to local repo: " + e.getMessage());
|
|
||||||
Log.e(TAG, Log.getStackTraceString(e));
|
|
||||||
return;
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Error adding app to local repo: " + e.getMessage());
|
Log.e(TAG, "Error adding app to local repo: " + e.getMessage());
|
||||||
Log.e(TAG, Log.getStackTraceString(e));
|
Log.e(TAG, Log.getStackTraceString(e));
|
||||||
return;
|
return;
|
||||||
@ -266,12 +273,8 @@ public class LocalRepoManager {
|
|||||||
apps.put(packageName, app);
|
apps.put(packageName, app);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeApp(String packageName) {
|
|
||||||
apps.remove(packageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getApps() {
|
public List<String> getApps() {
|
||||||
return new ArrayList<String>(apps.keySet());
|
return new ArrayList<>(apps.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void copyIconsToRepo() {
|
public void copyIconsToRepo() {
|
||||||
@ -290,8 +293,6 @@ public class LocalRepoManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the icon from an APK and writes it to the repo as a PNG
|
* Extracts the icon from an APK and writes it to the repo as a PNG
|
||||||
*
|
|
||||||
* @return path to the PNG file
|
|
||||||
*/
|
*/
|
||||||
public void copyIconToRepo(Drawable drawable, String packageName, int versionCode) {
|
public void copyIconToRepo(Drawable drawable, String packageName, int versionCode) {
|
||||||
Bitmap bitmap;
|
Bitmap bitmap;
|
||||||
@ -319,8 +320,6 @@ public class LocalRepoManager {
|
|||||||
return new File(iconsDir, packageName + "_" + versionCode + ".png");
|
return new File(iconsDir, packageName + "_" + versionCode + ".png");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this needs to be ported to < android-8
|
|
||||||
@TargetApi(8)
|
|
||||||
private void writeIndexXML() throws TransformerException, ParserConfigurationException, LocalRepoKeyStore.InitException {
|
private void writeIndexXML() throws TransformerException, ParserConfigurationException, LocalRepoKeyStore.InitException {
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
@ -338,7 +337,7 @@ public class LocalRepoManager {
|
|||||||
Element repo = doc.createElement("repo");
|
Element repo = doc.createElement("repo");
|
||||||
repo.setAttribute("icon", "blah.png");
|
repo.setAttribute("icon", "blah.png");
|
||||||
repo.setAttribute("maxage", String.valueOf(repoMaxAge));
|
repo.setAttribute("maxage", String.valueOf(repoMaxAge));
|
||||||
repo.setAttribute("name", repoName + " on " + ipAddressString);
|
repo.setAttribute("name", repoName + " on " + FDroidApp.ipAddressString);
|
||||||
repo.setAttribute("pubkey", Hasher.hex(LocalRepoKeyStore.get(context).getCertificate()));
|
repo.setAttribute("pubkey", Hasher.hex(LocalRepoKeyStore.get(context).getCertificate()));
|
||||||
long timestamp = System.currentTimeMillis() / 1000L;
|
long timestamp = System.currentTimeMillis() / 1000L;
|
||||||
repo.setAttribute("timestamp", String.valueOf(timestamp));
|
repo.setAttribute("timestamp", String.valueOf(timestamp));
|
||||||
@ -512,7 +511,7 @@ public class LocalRepoManager {
|
|||||||
} catch (LocalRepoKeyStore.InitException e) {
|
} catch (LocalRepoKeyStore.InitException e) {
|
||||||
throw new IOException("Could not sign index - keystore failed to initialize");
|
throw new IOException("Could not sign index - keystore failed to initialize");
|
||||||
} finally {
|
} finally {
|
||||||
xmlIndexJarUnsigned.delete();
|
attemptToDelete(xmlIndexJarUnsigned);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,6 @@ public class WifiStateChangeService extends Service {
|
|||||||
|
|
||||||
Context context = WifiStateChangeService.this.getApplicationContext();
|
Context context = WifiStateChangeService.this.getApplicationContext();
|
||||||
LocalRepoManager lrm = LocalRepoManager.get(context);
|
LocalRepoManager lrm = LocalRepoManager.get(context);
|
||||||
lrm.setUriString(FDroidApp.repo.address);
|
|
||||||
lrm.writeIndexPage(Utils.getSharingUri(context, FDroidApp.repo).toString());
|
lrm.writeIndexPage(Utils.getSharingUri(context, FDroidApp.repo).toString());
|
||||||
|
|
||||||
if (isCancelled())
|
if (isCancelled())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user