Skip to content

Commit 53d17da

Browse files
FIX ERRORS, AND ADD CUSTOM DATA DIR TO CONFIG
1 parent db38bad commit 53d17da

8 files changed

Lines changed: 136 additions & 82 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android {
1212
targetSdk 33
1313
versionCode 1
1414
multiDexEnabled true
15-
versionName "1.0"
15+
versionName "1.1-beta"
1616

1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
}

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
tools:targetApi="31">
1717

1818

19-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ1" android:label="Z1" android:name=".instances.ZI001" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
20-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ2" android:label="Z2" android:name=".instances.ZI002" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
21-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ3" android:label="Z3" android:name=".instances.ZI003" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
22-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ4" android:label="Z4" android:name=".instances.ZI004" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
23-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ5" android:label="Z5" android:name=".instances.ZI005" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
24-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ6" android:label="Z6" android:name=".instances.ZI006" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
25-
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ7" android:label="Z7" android:name=".instances.ZI007" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
26-
<activity android:autoRemoveFromRecents="true" android:name="br.com.nullexcept.webappmanager.app.MainActivity"
19+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ1" android:label="Z1" android:name=".instances.ZI001" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
20+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ2" android:label="Z2" android:name=".instances.ZI002" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
21+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ3" android:label="Z3" android:name=".instances.ZI003" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
22+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ4" android:label="Z4" android:name=".instances.ZI004" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
23+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ5" android:label="Z5" android:name=".instances.ZI005" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
24+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ6" android:label="Z6" android:name=".instances.ZI006" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
25+
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ7" android:label="Z7" android:name=".instances.ZI007" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
26+
27+
28+
<activity android:name="br.com.nullexcept.webappmanager.app.MainActivity"
2729
android:exported="true">
2830
<intent-filter>
2931
<action android:name="android.intent.action.MAIN"/>

app/src/main/java/br/com/nullexcept/webappmanager/app/BaseWebAppActivity.java

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Environment;
1111
import android.os.Process;
1212
import android.view.Window;
13+
import android.widget.Toast;
1314

1415
import androidx.annotation.NonNull;
1516
import androidx.annotation.Nullable;
@@ -40,8 +41,17 @@ public class BaseWebAppActivity extends AppCompatActivity {
4041
protected void onCreate(@Nullable Bundle savedInstanceState) {
4142
super.onCreate(savedInstanceState);
4243

43-
config = new Config(getClass().getName());
44-
config.load(getSharedPreferences("webapps",MODE_PRIVATE));
44+
config = new Config(this, getClass().getName());
45+
config.load();
46+
if (!config.enable){
47+
Toast.makeText(this, R.string.not_exists, Toast.LENGTH_LONG).show();
48+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
49+
finishAndRemoveTask();
50+
} else {
51+
finish();
52+
}
53+
return;
54+
}
4555

4656

4757
setTitle(config.name);
@@ -52,37 +62,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5262

5363
GeckoRuntimeSettings.Builder settings = new GeckoRuntimeSettings.Builder();
5464
settings = settings.arguments(new String[]{
55-
"profile", getFilesDir().getAbsolutePath(),
56-
"--profile", getFilesDir().getAbsolutePath()
65+
"--profile", config.getProfileDir().getAbsolutePath()
5766
});
5867

68+
session.getSettings().setUserAgentOverride(config.user_agent);
69+
5970
runtime = GeckoRuntime.create(this, settings.build());
6071
session.open(runtime);
61-
new Thread(()->{
62-
try {
63-
File dir = new File(getFilesDir().getParent(), "icon.png");
64-
dir.getParentFile().mkdirs();
65-
if (dir.exists()){
66-
loadIcon();
67-
return;
68-
}
69-
String url = config.url.substring(config.url.indexOf("://")+3);
70-
if (url.contains("/")){
71-
url = url.substring(0, url.indexOf("/"));
72-
}
73-
URLConnection connection = new URL((config.url.startsWith("https") ? "https" : "http")+"://"+url+"/favicon.ico").openConnection();
74-
connection.connect();
75-
InputStream down = connection.getInputStream();
76-
Bitmap bitmap = BitmapFactory.decodeStream(down);
77-
dir.delete();
78-
if (bitmap.getHeight() < 1 || bitmap.getWidth() < 1)return;
79-
FileOutputStream o = new FileOutputStream(dir);
80-
bitmap.compress(Bitmap.CompressFormat.PNG, 100, o);
81-
o.close();
82-
bitmap.recycle();
83-
loadIcon();
84-
}catch (Exception e){e.printStackTrace();}
85-
}).start();
8672
view.setSession(session);
8773

8874
session.loadUri(config.url);
@@ -92,10 +78,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9278
setTaskDescription(new ActivityManager.TaskDescription(config.name, null, Color.WHITE));
9379
}
9480
setTitle(config.name);
81+
checkIcon();
9582
}
9683

9784
private void loadIcon() {
98-
File dir = new File(getFilesDir().getParent(), "icon.png");
85+
File dir = new File(config.getSaveDir(), "icon.png");
9986
try {
10087
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
10188
setTaskDescription(new ActivityManager.TaskDescription(config.name, BitmapFactory.decodeFile(dir.getAbsolutePath()), Color.WHITE));
@@ -108,26 +95,13 @@ private void loadIcon() {
10895
@Override
10996
public void onBackPressed() {
11097
session.goBack();
111-
if(System.currentTimeMillis() - oldTime < 350){
98+
//Check double back click
99+
if(System.currentTimeMillis() - oldTime < 500){
112100
super.onBackPressed();
113101
}
114102
oldTime = System.currentTimeMillis();
115103
}
116104

117-
@Override
118-
public File getFilesDir() {
119-
File file = new File(super.getFilesDir(), "instances/"+getClass().getName()+"/files");
120-
file.mkdirs();
121-
return file;
122-
}
123-
124-
@Override
125-
public ApplicationInfo getApplicationInfo() {
126-
ApplicationInfo info = super.getApplicationInfo();
127-
info.dataDir = getFilesDir().getAbsolutePath();
128-
return info;
129-
}
130-
131105
@Override
132106
protected void onPause() {
133107
super.onPause();
@@ -144,8 +118,27 @@ protected void onDestroy() {
144118

145119
@Override
146120
public File getCacheDir() {
147-
File file = new File(getFilesDir().getParent(), "cache");
121+
File file = new File(config.getSaveDir(), "cache");
148122
file.mkdirs();
149123
return file;
150124
}
125+
126+
private void checkIcon(){
127+
new Thread(()->{
128+
try {
129+
File dir = new File(config.getSaveDir(), "icon.png");
130+
dir.getParentFile().mkdirs();
131+
if (dir.exists()){
132+
loadIcon();
133+
return;
134+
}
135+
136+
String url = config.url.substring(config.url.indexOf("://")+3);
137+
if (url.contains("/")){
138+
url = url.substring(0, url.indexOf("/"));
139+
}
140+
//@TODO Need add code to get Icon from GeckoSession
141+
}catch (Exception e){e.printStackTrace();}
142+
}).start();
143+
}
151144
}

app/src/main/java/br/com/nullexcept/webappmanager/app/MainActivity.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.File;
3434
import java.util.HashMap;
35+
import java.util.Objects;
3536

3637
import br.com.nullexcept.webappmanager.R;
3738
import br.com.nullexcept.webappmanager.config.Config;
@@ -64,17 +65,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6465
if (savedInstanceState == null){
6566
loadList();
6667
findViewById(R.id.create).setOnClickListener((vw)->{
67-
String key = null;
68-
for (String ky:WEBAPPS.keySet()){
69-
if(!WEBAPPS.get(ky).enable){
70-
key = ky;
71-
break;
72-
}
73-
}
74-
showDialog(new Config(key));
68+
showDialog(new Config(this, findLastFreeSlot()));
7569
});
7670
}
7771

72+
findLastFreeSlot();
73+
}
74+
75+
public String findLastFreeSlot(){
76+
String key = null;
77+
for (String ky:WEBAPPS.keySet()){
78+
if(!Objects.requireNonNull(WEBAPPS.get(ky)).enable){
79+
key = ky;
80+
break;
81+
} else System.out.println(">> Slot ["+ky+"] is in use.");
82+
}
83+
return key;
7884
}
7985

8086
private void showDialog(Config config){
@@ -142,10 +148,10 @@ private void createShortcut(Config config, Intent intent) {
142148
private void checkAndCreateDatabase() {
143149
for (Class clazz: LIST){
144150
SharedPreferences prefs = getSharedPreferences("webapps", Context.MODE_PRIVATE);
145-
Config config = new Config(clazz.getName());
146-
config.load(prefs);
151+
Config config = new Config(this, clazz.getName());
152+
config.load();
147153
WEBAPPS.put(clazz.getName(), config);
148-
config.save(prefs);
154+
config.save();
149155
}
150156
}
151157

app/src/main/java/br/com/nullexcept/webappmanager/config/Config.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package br.com.nullexcept.webappmanager.config;
22

3+
import android.app.Activity;
4+
import android.app.Application;
5+
import android.content.Context;
36
import android.content.SharedPreferences;
7+
import android.content.pm.ApplicationInfo;
8+
import android.os.Environment;
49

510
import org.mozilla.geckoview.GeckoSession;
611
import org.mozilla.geckoview.GeckoView;
712

13+
import java.io.File;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
17+
import br.com.nullexcept.webappmanager.util.FileUtils;
18+
819
public class Config {
920
private final String base;
1021

@@ -14,17 +25,48 @@ public class Config {
1425
public boolean action_bar = false;
1526
public boolean enable = false;
1627
public boolean redirects = false;
28+
public File data_directory;
29+
public Context ctx;
1730

18-
public Config(String base){
31+
public Config(Activity ctx, String base){
1932
this.base = base;
33+
this.data_directory = ctx.getFilesDir();
34+
this.ctx = ctx.getApplication();
35+
System.out.println("Data directory: "+ ctx.getFilesDir());
36+
}
37+
38+
public File getSaveDir(){
39+
return mkdirs(new File(data_directory, "instances/"+base));
40+
}
41+
42+
private File mkdirs(File file){
43+
file.mkdirs();
44+
return file;
2045
}
2146

47+
public File getProfileDir(){
48+
return mkdirs(new File(getSaveDir(), "profile"));
49+
}
50+
51+
public void deleteAll(){
52+
ArrayList<File> baseDirectory = new ArrayList<>();
53+
baseDirectory.addAll(Arrays.asList(ctx.getExternalFilesDirs(null)));
54+
baseDirectory.add(ctx.getFilesDir());
55+
baseDirectory.add(this.data_directory);
56+
for (File file: baseDirectory){
57+
FileUtils.delete(new File(file, "instances/"+base));
58+
}
59+
enable = false;
60+
save();
61+
}
2262

2363
public String getBase() {
2464
return base;
2565
}
2666

27-
public void load(SharedPreferences prefs){
67+
public void load(){
68+
SharedPreferences prefs = ctx.getSharedPreferences("webapps",Context.MODE_PRIVATE);
69+
2870
enable = prefs.getBoolean(base, enable);
2971

3072
name = prefs.getString(base+"/name", name);
@@ -35,7 +77,8 @@ public void load(SharedPreferences prefs){
3577
redirects = prefs.getBoolean(base+"/redirects", redirects);
3678
}
3779

38-
public void save(SharedPreferences prefs){
80+
public void save(){
81+
SharedPreferences prefs = ctx.getSharedPreferences("webapps",Context.MODE_PRIVATE);
3982
prefs.edit()
4083
.putString(base+"/name", name)
4184
.putString(base+"/user_agent", user_agent)

app/src/main/java/br/com/nullexcept/webappmanager/dialog/EditWebAppDialog.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public EditWebAppDialog(Config config, MainActivity ctx){
7575
if (config.enable){
7676
layout.findViewById(R.id.delete).setVisibility(View.VISIBLE);
7777
layout.findViewById(R.id.delete).setOnClickListener(v -> {
78+
clearAllData();
7879
dialog.hide();
7980
config.enable = false;
80-
config.save(ctx.getSharedPreferences("webapps", MODE_PRIVATE));
81+
config.save();
8182
ctx.refreshList();
82-
clearAllData();
8383
});
8484
} else {
8585
layout.findViewById(R.id.delete).setVisibility(View.INVISIBLE);
@@ -97,30 +97,22 @@ private void save() {
9797
dialog.hide();
9898
config.name = name.getText()+"";
9999
config.url = url.getText()+"";
100+
config.user_agent = user_agent.getText()+"";
100101
config.action_bar = show_action.isChecked();
101102
config.redirects = allow_redirects.isChecked();
102103
if (!config.enable){
103104
clearAllData();
104105
config.enable = true;
105106
}
106107
MainActivity.WEBAPPS.put(config.getBase(), config);
107-
config.save(ctx.getSharedPreferences("webapps", MODE_PRIVATE));
108+
config.save();
108109
ctx.refreshList();
109110
}
110111

111112
private void clearAllData() {
112-
File data = new File(ctx.getFilesDir(), "instances/"+config.getBase());
113-
data.mkdirs();
114-
deleteRecursive(data);
115-
data.mkdirs();
113+
config.deleteAll();
116114
}
117-
void deleteRecursive(File fileOrDirectory) {
118-
if (fileOrDirectory.isDirectory())
119-
for (File child : fileOrDirectory.listFiles())
120-
deleteRecursive(child);
121115

122-
fileOrDirectory.delete();
123-
}
124116

125117
private boolean touchUpdate(View vw, MotionEvent motion){
126118
checkAndContinue();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package br.com.nullexcept.webappmanager.util;
2+
3+
import java.io.File;
4+
import java.util.Objects;
5+
6+
public class FileUtils {
7+
8+
//@TODO Delete recursive files.
9+
public static void delete(File file){
10+
if (file.isDirectory()){
11+
for (File child: Objects.requireNonNull(file.listFiles())){
12+
delete(child);
13+
}
14+
}
15+
file.delete();
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">WebApp Manager</string>
33
<string name="make_shortcut">Shortcut created</string>
4+
<string name="not_exists">This WebApp not exists.</string>
45
</resources>

0 commit comments

Comments
 (0)