Skip to content

Commit 025d66d

Browse files
committed
add updated DefaultExtractPolicy to project template
1 parent 65e6d1e commit 025d66d

1 file changed

Lines changed: 42 additions & 53 deletions

File tree

build/project-template-gradle/src/main/java/com/tns/DefaultExtractPolicy.java

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,122 +13,111 @@
1313
import android.content.Context;
1414
import android.content.pm.PackageInfo;
1515
import android.content.pm.PackageManager;
16+
import android.content.pm.PackageManager.NameNotFoundException;
1617

1718
import com.tns.Logger;
1819
import com.tns.ExtractPolicy;
1920
import com.tns.FileExtractor;
2021

21-
public class DefaultExtractPolicy implements ExtractPolicy
22-
{
22+
public class DefaultExtractPolicy implements ExtractPolicy {
2323
private final Logger logger;
2424

2525
private final static String ASSETS_THUMB_FILENAME = "assetsThumb";
2626

27-
public DefaultExtractPolicy(Logger logger)
28-
{
27+
public DefaultExtractPolicy(Logger logger) {
2928
this.logger = logger;
3029
}
3130

32-
public boolean shouldExtract(android.content.Context context)
33-
{
34-
String assetsThumb = generateAssetsThumb(context);
35-
if (assetsThumb != null)
36-
{
37-
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
38-
String oldAssetsThumb = getCachedAssetsThumb(assetsThumbFilePath);
39-
if (oldAssetsThumb == null || !assetsThumb.equals(oldAssetsThumb))
40-
{
41-
saveNewAssetsThumb(assetsThumb, assetsThumbFilePath);
31+
public boolean shouldExtract(Context context)
32+
{
33+
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
34+
String oldAssetsThumb = getCachedAssetsThumb(assetsThumbFilePath);
35+
if (oldAssetsThumb == null) {
36+
return true;
37+
} else {
38+
String currentThumb = getAssetsThumb(context);
39+
40+
if(currentThumb != null && !currentThumb.equals(oldAssetsThumb)) {
4241
return true;
4342
}
4443
}
45-
44+
4645
return false;
4746
}
4847

49-
public boolean forceOverwrite()
50-
{
48+
public void setAssetsThumb(Context context) {
49+
String assetsThumb = generateAssetsThumb(context);
50+
if(assetsThumb != null) {
51+
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
52+
saveNewAssetsThumb(assetsThumb, assetsThumbFilePath);
53+
}
54+
}
55+
56+
public boolean forceOverwrite() {
5157
return true;
5258
}
5359

54-
public FileExtractor extractor()
55-
{
60+
public FileExtractor extractor() {
5661
return null;
5762
}
5863

59-
private String generateAssetsThumb(Context context)
60-
{
61-
try
62-
{
64+
public String getAssetsThumb(Context context) {
65+
return generateAssetsThumb(context);
66+
}
67+
68+
private String generateAssetsThumb(Context context) {
69+
try {
6370
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
6471
int code = packageInfo.versionCode;
6572
long updateTime = packageInfo.lastUpdateTime;
6673
return String.valueOf(updateTime) + "-" + String.valueOf(code);
67-
}
68-
catch (PackageManager.NameNotFoundException e)
69-
{
74+
} catch (PackageManager.NameNotFoundException e) {
7075
logger.write("Error while getting current assets thumb");
7176
e.printStackTrace();
7277
}
7378

7479
return null;
7580
}
7681

77-
private String getCachedAssetsThumb(String assetsThumbFilePath)
78-
{
79-
try
80-
{
82+
private String getCachedAssetsThumb(String assetsThumbFilePath) {
83+
try {
8184
File cachedThumbFile = new File(assetsThumbFilePath);
82-
if (cachedThumbFile.exists())
83-
{
85+
if (cachedThumbFile.exists()) {
8486
FileInputStream in = new FileInputStream(cachedThumbFile);
8587
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
8688
String cachedThumb = reader.readLine();
8789
reader.close();
8890
in.close();
8991
return cachedThumb;
9092
}
91-
}
92-
catch (FileNotFoundException e)
93-
{
93+
} catch (FileNotFoundException e) {
9494
logger.write("Error while getting current assets thumb");
9595
e.printStackTrace();
96-
}
97-
catch (IOException e)
98-
{
96+
} catch (IOException e) {
9997
logger.write("Error while getting current asstes thumb");
10098
e.printStackTrace();
10199
}
102100

103101
return null;
104102
}
105103

106-
private void saveNewAssetsThumb(String newThumb, String assetsThumbFile)
107-
{
104+
private void saveNewAssetsThumb(String newThumb, String assetsThumbFile) {
108105
File cachedThumbFile = new File(assetsThumbFile);
109-
try
110-
{
106+
try {
111107
FileOutputStream out = new FileOutputStream(cachedThumbFile, false);
112108
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
113-
try
114-
{
109+
try {
115110
writer.write(newThumb);
116111
writer.newLine();
117112
writer.flush();
118-
}
119-
finally
120-
{
113+
} finally {
121114
writer.close();
122115
out.close();
123116
}
124-
}
125-
catch (FileNotFoundException e)
126-
{
117+
} catch (FileNotFoundException e) {
127118
logger.write("Error while writting current assets thumb");
128119
e.printStackTrace();
129-
}
130-
catch (IOException e)
131-
{
120+
} catch (IOException e) {
132121
logger.write("Error while writting current assets thumb");
133122
e.printStackTrace();
134123
}

0 commit comments

Comments
 (0)