-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+SAI can now open .apks files (renamed zips) and install APKs from them
*rewrote apks supplying to installers scheme
- Loading branch information
Showing
19 changed files
with
565 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 8 additions & 124 deletions
132
app/src/main/java/com/aefyr/sai/installer/QueuedInstallation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,31 @@ | ||
package com.aefyr.sai.installer; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import com.aefyr.pseudoapksigner.PseudoApkSigner; | ||
import com.aefyr.sai.R; | ||
import com.aefyr.sai.utils.IOUtils; | ||
import com.aefyr.sai.model.apksource.ApkSource; | ||
import com.aefyr.sai.model.apksource.SignerApkSource; | ||
import com.aefyr.sai.utils.PreferencesHelper; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
class QueuedInstallation { | ||
private static final String TAG = "SAIQueuedInstallation"; | ||
private static final String FILE_NAME_PAST = "testkey.past"; | ||
private static final String FILE_NAME_PRIVATE_KEY = "testkey.pk8"; | ||
|
||
private Context mContext; | ||
private File mZipWithApkFiles; | ||
private boolean mShouldExtractZip; | ||
private List<File> mApkFiles; | ||
private File mCacheDirectory; | ||
private ApkSource mApkSource; | ||
private long mId; | ||
|
||
QueuedInstallation(Context c, List<File> apkFiles, long id) { | ||
mContext = c; | ||
mApkFiles = apkFiles; | ||
mId = id; | ||
} | ||
|
||
QueuedInstallation(Context c, File zipWithApkFiles, long id) { | ||
QueuedInstallation(Context c, ApkSource apkSource, long id) { | ||
mContext = c; | ||
mZipWithApkFiles = zipWithApkFiles; | ||
mShouldExtractZip = true; | ||
mApkSource = apkSource; | ||
mId = id; | ||
} | ||
|
||
long getId() { | ||
return mId; | ||
} | ||
|
||
List<File> getApkFiles() throws Exception { | ||
if (mShouldExtractZip) | ||
extractZip(); | ||
|
||
ApkSource getApkSource() { | ||
if (PreferencesHelper.getInstance(mContext).shouldSignApks()) | ||
signApks(); | ||
|
||
return mApkFiles; | ||
} | ||
|
||
void clear() { | ||
if (mCacheDirectory != null) { | ||
deleteFile(mCacheDirectory); | ||
} | ||
} | ||
|
||
private void deleteFile(File f) { | ||
if (f.isDirectory()) { | ||
for (File child : f.listFiles()) | ||
deleteFile(child); | ||
} | ||
f.delete(); | ||
} | ||
|
||
private void extractZip() throws Exception { | ||
createCacheDir(); | ||
|
||
ZipFile zipFile = new ZipFile(mZipWithApkFiles); | ||
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | ||
mApkFiles = new ArrayList<>(zipFile.size()); | ||
|
||
while (entries.hasMoreElements()) { | ||
ZipEntry entry = entries.nextElement(); | ||
|
||
if (entry.isDirectory() || !entry.getName().endsWith(".apk")) | ||
throw new IllegalArgumentException(mContext.getString(R.string.installer_error_zip_contains_non_apks)); | ||
|
||
|
||
File tempApkFile = new File(mCacheDirectory, entry.getName()); | ||
|
||
FileOutputStream outputStream = new FileOutputStream(tempApkFile); | ||
InputStream inputStream = zipFile.getInputStream(entry); | ||
IOUtils.copyStream(inputStream, outputStream); | ||
|
||
outputStream.close(); | ||
inputStream.close(); | ||
|
||
mApkFiles.add(tempApkFile); | ||
} | ||
zipFile.close(); | ||
} | ||
|
||
private void signApks() throws Exception { | ||
if (mCacheDirectory == null) | ||
createCacheDir(); | ||
|
||
checkAndPrepareSigningEnvironment(); | ||
|
||
ArrayList<File> originalApkFiles = new ArrayList<>(mApkFiles); | ||
mApkFiles.clear(); | ||
|
||
PseudoApkSigner apkSigner = new PseudoApkSigner(new File(getSigningEnvironmentDir(), FILE_NAME_PAST), new File(getSigningEnvironmentDir(), FILE_NAME_PRIVATE_KEY)); | ||
for (File apkFile : originalApkFiles) { | ||
String rawFileName = apkFile.getName(); | ||
int indexOfLastDot = rawFileName.lastIndexOf('.'); | ||
String fileName = rawFileName.substring(0, indexOfLastDot); | ||
String fileExtension = rawFileName.substring(indexOfLastDot + 1); | ||
|
||
File signedApkFile = new File(mCacheDirectory, String.format("%s_signed.%s", fileName, fileExtension)); | ||
apkSigner.sign(apkFile, signedApkFile); | ||
|
||
mApkFiles.add(signedApkFile); | ||
} | ||
} | ||
|
||
private void checkAndPrepareSigningEnvironment() throws Exception { | ||
File signingEnvironment = getSigningEnvironmentDir(); | ||
File pastFile = new File(signingEnvironment, FILE_NAME_PAST); | ||
File privateKeyFile = new File(signingEnvironment, FILE_NAME_PRIVATE_KEY); | ||
|
||
if (pastFile.exists() && privateKeyFile.exists()) | ||
return; | ||
|
||
Log.d(TAG, "Preparing signing environment..."); | ||
signingEnvironment.mkdir(); | ||
|
||
IOUtils.copyFileFromAssets(mContext, FILE_NAME_PAST, pastFile); | ||
IOUtils.copyFileFromAssets(mContext, FILE_NAME_PRIVATE_KEY, privateKeyFile); | ||
} | ||
|
||
private File getSigningEnvironmentDir() { | ||
return new File(mContext.getFilesDir(), "signing"); | ||
} | ||
return new SignerApkSource(mContext, mApkSource); | ||
|
||
private void createCacheDir() { | ||
mCacheDirectory = new File(mContext.getCacheDir(), String.valueOf(System.currentTimeMillis())); | ||
mCacheDirectory.mkdirs(); | ||
return mApkSource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.