-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
278 additions
and
12 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
154 changes: 154 additions & 0 deletions
154
app/src/main/java/com/saradabar/cpadcustomizetool/data/task/ApkSCopyTask.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 |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.saradabar.cpadcustomizetool.data.task; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.saradabar.cpadcustomizetool.R; | ||
import com.saradabar.cpadcustomizetool.util.Common; | ||
|
||
import org.zeroturnaround.zip.ZipUtil; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ApkSCopyTask { | ||
|
||
long totalByte = 0; | ||
|
||
public void execute(Context context, Listener listener, ArrayList<String> splitInstallData) { | ||
onPreExecute(listener); | ||
Executors.newSingleThreadExecutor().submit(() -> { | ||
Handler handler = new Handler(Looper.getMainLooper()); | ||
new Thread(() -> { | ||
Object result = doInBackground(context, listener, splitInstallData); | ||
handler.post(() -> onPostExecute(context, listener, result)); | ||
}).start(); | ||
}); | ||
} | ||
|
||
void onPreExecute(Listener listener) { | ||
listener.onShow(); | ||
} | ||
|
||
void onPostExecute(Context context, Listener listener, Object result) { | ||
if (result == null) { | ||
totalByte = -1; | ||
listener.onError(context.getString(R.string.installer_status_unknown_error)); | ||
return; | ||
} | ||
|
||
if (result.getClass() == ArrayList.class) { | ||
ArrayList<String> stringArrayList = new ArrayList<>(); | ||
|
||
for (Object o : (Iterable<?>) result) { | ||
stringArrayList.add((String) o); | ||
} | ||
|
||
totalByte = -1; | ||
listener.onSuccess(stringArrayList); | ||
return; | ||
} | ||
|
||
if (result.equals(false)) { | ||
totalByte = -1; | ||
listener.onFailure(); | ||
return; | ||
} | ||
|
||
totalByte = -1; | ||
listener.onError(result.toString()); | ||
} | ||
|
||
public void onProgressUpdate(Listener listener, String message) { | ||
listener.onProgressUpdate(message); | ||
} | ||
|
||
protected Object doInBackground(Context context, Listener listener, ArrayList<String> splitInstallData) { | ||
//noinspection SequencedCollectionMethodCanBeUsed | ||
String zipFile = splitInstallData.get(0); | ||
File tmpFile = new File(Common.getTemporaryPath(context)); | ||
totalByte = new File(zipFile).length(); | ||
|
||
// apksのファイルパスを削除 | ||
//noinspection SequencedCollectionMethodCanBeUsed | ||
splitInstallData.remove(0); | ||
|
||
/* zipを展開して外部ディレクトリに一時保存 */ | ||
onProgressUpdate(listener, context.getString(R.string.progress_state_unpack)); | ||
|
||
try { | ||
ZipUtil.unpack(new File(zipFile), tmpFile); | ||
// .apks を展開した splits の中が .apk 群 | ||
tmpFile = new File(Common.getTemporaryPath(context) + "/splits"); | ||
} catch (Exception e) { | ||
return e.getMessage(); | ||
} | ||
|
||
File[] zipListFiles = tmpFile.listFiles(); | ||
|
||
if (zipListFiles != null) { | ||
int c = 0; | ||
|
||
/* ディレクトリのなかのファイルを取得 */ | ||
for (int i = 0; i < zipListFiles.length; i++) { | ||
if (zipListFiles[i].isDirectory()) { | ||
c++; | ||
} else { | ||
onProgressUpdate(listener, context.getString(R.string.progress_state_check_file)); | ||
zipFile = zipListFiles[i].getName(); | ||
|
||
/* apkファイルならパスをインストールデータへ */ | ||
if (zipFile.substring(zipFile.lastIndexOf(".")).equalsIgnoreCase(".apk") | ||
&& !zipFile.endsWith("master_2.apk")) { | ||
splitInstallData.add(i - c, zipListFiles[i].getPath()); | ||
} else { | ||
/* apkファイルでなかったときのリストの順番を修正 */ | ||
c++; | ||
} | ||
} | ||
} | ||
return splitInstallData; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public interface Listener { | ||
void onShow(); | ||
|
||
void onSuccess(ArrayList<String> splitInstallData); | ||
|
||
void onFailure(); | ||
|
||
void onError(String message); | ||
|
||
void onProgressUpdate(String message); | ||
} | ||
|
||
public int getLoadedBytePercent(Context context) { | ||
if (totalByte <= 0) { | ||
return 0; | ||
} | ||
return (int) Math.floor(((double) getLoadedCurrentByte(context) / getLoadedTotalByte()) * 100); | ||
} | ||
|
||
public int getLoadedTotalByte() { | ||
return (int) totalByte / (1024 * 1024); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.O) | ||
public int getLoadedCurrentByte(Context context) { | ||
if (totalByte <= 0) { | ||
return 0; | ||
} | ||
return (int) Common.getFileSize(new File(Common.getTemporaryPath(context))) / (1024 * 1024); | ||
} | ||
|
||
public boolean isFinish() { | ||
return totalByte == -1; | ||
} | ||
} |
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.