Skip to content

Commit

Permalink
FIX ERRORS, AND ADD CUSTOM DATA DIR TO CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Feb 10, 2023
1 parent db38bad commit 53d17da
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 82 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
targetSdk 33
versionCode 1
multiDexEnabled true
versionName "1.0"
versionName "1.1-beta"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
tools:targetApi="31">


<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ1" android:label="Z1" android:name=".instances.ZI001" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ2" android:label="Z2" android:name=".instances.ZI002" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ3" android:label="Z3" android:name=".instances.ZI003" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ4" android:label="Z4" android:name=".instances.ZI004" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ5" android:label="Z5" android:name=".instances.ZI005" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ6" android:label="Z6" android:name=".instances.ZI006" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ7" android:label="Z7" android:name=".instances.ZI007" android:exported="true" android:launchMode="singleTask" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:name="br.com.nullexcept.webappmanager.app.MainActivity"
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ1" android:label="Z1" android:name=".instances.ZI001" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ2" android:label="Z2" android:name=".instances.ZI002" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ3" android:label="Z3" android:name=".instances.ZI003" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ4" android:label="Z4" android:name=".instances.ZI004" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ5" android:label="Z5" android:name=".instances.ZI005" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ6" android:label="Z6" android:name=".instances.ZI006" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>
<activity android:autoRemoveFromRecents="true" android:process=":WebAppZ7" android:label="Z7" android:name=".instances.ZI007" android:exported="true" android:launchMode="standard" android:documentLaunchMode="intoExisting"/>


<activity android:name="br.com.nullexcept.webappmanager.app.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Environment;
import android.os.Process;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -40,8 +41,17 @@ public class BaseWebAppActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

config = new Config(getClass().getName());
config.load(getSharedPreferences("webapps",MODE_PRIVATE));
config = new Config(this, getClass().getName());
config.load();
if (!config.enable){
Toast.makeText(this, R.string.not_exists, Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask();
} else {
finish();
}
return;
}


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

GeckoRuntimeSettings.Builder settings = new GeckoRuntimeSettings.Builder();
settings = settings.arguments(new String[]{
"profile", getFilesDir().getAbsolutePath(),
"--profile", getFilesDir().getAbsolutePath()
"--profile", config.getProfileDir().getAbsolutePath()
});

session.getSettings().setUserAgentOverride(config.user_agent);

runtime = GeckoRuntime.create(this, settings.build());
session.open(runtime);
new Thread(()->{
try {
File dir = new File(getFilesDir().getParent(), "icon.png");
dir.getParentFile().mkdirs();
if (dir.exists()){
loadIcon();
return;
}
String url = config.url.substring(config.url.indexOf("://")+3);
if (url.contains("/")){
url = url.substring(0, url.indexOf("/"));
}
URLConnection connection = new URL((config.url.startsWith("https") ? "https" : "http")+"://"+url+"/favicon.ico").openConnection();
connection.connect();
InputStream down = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(down);
dir.delete();
if (bitmap.getHeight() < 1 || bitmap.getWidth() < 1)return;
FileOutputStream o = new FileOutputStream(dir);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, o);
o.close();
bitmap.recycle();
loadIcon();
}catch (Exception e){e.printStackTrace();}
}).start();
view.setSession(session);

session.loadUri(config.url);
Expand All @@ -92,10 +78,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setTaskDescription(new ActivityManager.TaskDescription(config.name, null, Color.WHITE));
}
setTitle(config.name);
checkIcon();
}

private void loadIcon() {
File dir = new File(getFilesDir().getParent(), "icon.png");
File dir = new File(config.getSaveDir(), "icon.png");
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setTaskDescription(new ActivityManager.TaskDescription(config.name, BitmapFactory.decodeFile(dir.getAbsolutePath()), Color.WHITE));
Expand All @@ -108,26 +95,13 @@ private void loadIcon() {
@Override
public void onBackPressed() {
session.goBack();
if(System.currentTimeMillis() - oldTime < 350){
//Check double back click
if(System.currentTimeMillis() - oldTime < 500){
super.onBackPressed();
}
oldTime = System.currentTimeMillis();
}

@Override
public File getFilesDir() {
File file = new File(super.getFilesDir(), "instances/"+getClass().getName()+"/files");
file.mkdirs();
return file;
}

@Override
public ApplicationInfo getApplicationInfo() {
ApplicationInfo info = super.getApplicationInfo();
info.dataDir = getFilesDir().getAbsolutePath();
return info;
}

@Override
protected void onPause() {
super.onPause();
Expand All @@ -144,8 +118,27 @@ protected void onDestroy() {

@Override
public File getCacheDir() {
File file = new File(getFilesDir().getParent(), "cache");
File file = new File(config.getSaveDir(), "cache");
file.mkdirs();
return file;
}

private void checkIcon(){
new Thread(()->{
try {
File dir = new File(config.getSaveDir(), "icon.png");
dir.getParentFile().mkdirs();
if (dir.exists()){
loadIcon();
return;
}

String url = config.url.substring(config.url.indexOf("://")+3);
if (url.contains("/")){
url = url.substring(0, url.indexOf("/"));
}
//@TODO Need add code to get Icon from GeckoSession
}catch (Exception e){e.printStackTrace();}
}).start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.File;
import java.util.HashMap;
import java.util.Objects;

import br.com.nullexcept.webappmanager.R;
import br.com.nullexcept.webappmanager.config.Config;
Expand Down Expand Up @@ -64,17 +65,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null){
loadList();
findViewById(R.id.create).setOnClickListener((vw)->{
String key = null;
for (String ky:WEBAPPS.keySet()){
if(!WEBAPPS.get(ky).enable){
key = ky;
break;
}
}
showDialog(new Config(key));
showDialog(new Config(this, findLastFreeSlot()));
});
}

findLastFreeSlot();
}

public String findLastFreeSlot(){
String key = null;
for (String ky:WEBAPPS.keySet()){
if(!Objects.requireNonNull(WEBAPPS.get(ky)).enable){
key = ky;
break;
} else System.out.println(">> Slot ["+ky+"] is in use.");
}
return key;
}

private void showDialog(Config config){
Expand Down Expand Up @@ -142,10 +148,10 @@ private void createShortcut(Config config, Intent intent) {
private void checkAndCreateDatabase() {
for (Class clazz: LIST){
SharedPreferences prefs = getSharedPreferences("webapps", Context.MODE_PRIVATE);
Config config = new Config(clazz.getName());
config.load(prefs);
Config config = new Config(this, clazz.getName());
config.load();
WEBAPPS.put(clazz.getName(), config);
config.save(prefs);
config.save();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package br.com.nullexcept.webappmanager.config;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.os.Environment;

import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import br.com.nullexcept.webappmanager.util.FileUtils;

public class Config {
private final String base;

Expand All @@ -14,17 +25,48 @@ public class Config {
public boolean action_bar = false;
public boolean enable = false;
public boolean redirects = false;
public File data_directory;
public Context ctx;

public Config(String base){
public Config(Activity ctx, String base){
this.base = base;
this.data_directory = ctx.getFilesDir();
this.ctx = ctx.getApplication();
System.out.println("Data directory: "+ ctx.getFilesDir());
}

public File getSaveDir(){
return mkdirs(new File(data_directory, "instances/"+base));
}

private File mkdirs(File file){
file.mkdirs();
return file;
}

public File getProfileDir(){
return mkdirs(new File(getSaveDir(), "profile"));
}

public void deleteAll(){
ArrayList<File> baseDirectory = new ArrayList<>();
baseDirectory.addAll(Arrays.asList(ctx.getExternalFilesDirs(null)));
baseDirectory.add(ctx.getFilesDir());
baseDirectory.add(this.data_directory);
for (File file: baseDirectory){
FileUtils.delete(new File(file, "instances/"+base));
}
enable = false;
save();
}

public String getBase() {
return base;
}

public void load(SharedPreferences prefs){
public void load(){
SharedPreferences prefs = ctx.getSharedPreferences("webapps",Context.MODE_PRIVATE);

enable = prefs.getBoolean(base, enable);

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

public void save(SharedPreferences prefs){
public void save(){
SharedPreferences prefs = ctx.getSharedPreferences("webapps",Context.MODE_PRIVATE);
prefs.edit()
.putString(base+"/name", name)
.putString(base+"/user_agent", user_agent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public EditWebAppDialog(Config config, MainActivity ctx){
if (config.enable){
layout.findViewById(R.id.delete).setVisibility(View.VISIBLE);
layout.findViewById(R.id.delete).setOnClickListener(v -> {
clearAllData();
dialog.hide();
config.enable = false;
config.save(ctx.getSharedPreferences("webapps", MODE_PRIVATE));
config.save();
ctx.refreshList();
clearAllData();
});
} else {
layout.findViewById(R.id.delete).setVisibility(View.INVISIBLE);
Expand All @@ -97,30 +97,22 @@ private void save() {
dialog.hide();
config.name = name.getText()+"";
config.url = url.getText()+"";
config.user_agent = user_agent.getText()+"";
config.action_bar = show_action.isChecked();
config.redirects = allow_redirects.isChecked();
if (!config.enable){
clearAllData();
config.enable = true;
}
MainActivity.WEBAPPS.put(config.getBase(), config);
config.save(ctx.getSharedPreferences("webapps", MODE_PRIVATE));
config.save();
ctx.refreshList();
}

private void clearAllData() {
File data = new File(ctx.getFilesDir(), "instances/"+config.getBase());
data.mkdirs();
deleteRecursive(data);
data.mkdirs();
config.deleteAll();
}
void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);

fileOrDirectory.delete();
}

private boolean touchUpdate(View vw, MotionEvent motion){
checkAndContinue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.nullexcept.webappmanager.util;

import java.io.File;
import java.util.Objects;

public class FileUtils {

//@TODO Delete recursive files.
public static void delete(File file){
if (file.isDirectory()){
for (File child: Objects.requireNonNull(file.listFiles())){
delete(child);
}
}
file.delete();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">WebApp Manager</string>
<string name="make_shortcut">Shortcut created</string>
<string name="not_exists">This WebApp not exists.</string>
</resources>

0 comments on commit 53d17da

Please sign in to comment.