mirror of
https://github.com/xoureldeen/Vectras-VM-Android.git
synced 2026-05-05 17:56:59 +00:00
2.8.4 still in development
This commit is contained in:
parent
9edcda8d1e
commit
f8f2a23bcd
2748 changed files with 515838 additions and 0 deletions
184
app/src/main/java/com/vectras/qemu/Config.java
Normal file
184
app/src/main/java/com/vectras/qemu/Config.java
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
Copyright (C) Max Kastanas 2012
|
||||
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
package com.vectras.qemu;
|
||||
|
||||
import android.androidVNC.COLORMODEL;
|
||||
import android.androidVNC.VncCanvasActivity;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Environment;
|
||||
import android.widget.ImageView.ScaleType;
|
||||
|
||||
import com.vectras.vm.AppConfig;
|
||||
import com.vectras.vm.SplashActivity;
|
||||
import com.vectras.vm.VectrasApp;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author dev
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
// Constants
|
||||
public static final int UI_VNC = 0;
|
||||
public static final int UI_SDL = 1;
|
||||
public static final int UI_SPICE = 2;
|
||||
public static final int SDL_MOUSE_LEFT = 1;
|
||||
public static final int SDL_MOUSE_MIDDLE = 2;
|
||||
public static final int SDL_MOUSE_RIGHT = 3;
|
||||
public static final int VNC_REQUEST_CODE = 1004;
|
||||
public static final int VNC_RESET_RESULT_CODE = 1006;
|
||||
public static final int SDL_REQUEST_CODE = 1007;
|
||||
public static final String ACTION_START = "com.vectras.qemu.action.STARTVM";
|
||||
|
||||
// GUI Options
|
||||
public static final boolean enable_SDL = true;
|
||||
public static final boolean enable_SPICE = false;
|
||||
|
||||
public static final boolean enable_qemu_fullScreen = true;
|
||||
|
||||
// App config
|
||||
public static final String APP_NAME = "Vectras Emulator";
|
||||
public static String storagedir = null;
|
||||
|
||||
//Some OSes don't like emulated multi cores for QEMU 2.9.1 you can disable here
|
||||
/// thought there is also the Disable TSC feature so you don't have to do it here
|
||||
public static boolean enableSMPOnlyOnKVM = false;
|
||||
|
||||
//set to true if you need to debug native library loading
|
||||
public static boolean loadNativeLibsEarly = false;
|
||||
|
||||
//XXX: QEMU 3.1.0 needs the libraries to be loaded from the main thread
|
||||
public static boolean loadNativeLibsMainThread = true;
|
||||
|
||||
public static String wakeLockTag = "vectras:wakelock";
|
||||
public static String wifiLockTag = "vectras:wifilock";
|
||||
|
||||
//this will be populated later
|
||||
public static String cacheDir = null;
|
||||
|
||||
//we disable mouse modes for now
|
||||
public static boolean disableMouseModes = true;
|
||||
|
||||
//double tap an hold is still buggy so we keep using the old-way trackpad
|
||||
public static boolean enableDragOnLongPress = true;
|
||||
|
||||
//we need to define the configuration for the VNC client since we replaced some deprecated
|
||||
// functions
|
||||
public static Bitmap.Config bitmapConfig = Bitmap.Config.RGB_565;
|
||||
|
||||
//XXX set scaling to linear it's a tad slower but it's worth it
|
||||
public static int SDLHintScale=1;
|
||||
public static boolean viewLogInternally = true;
|
||||
|
||||
|
||||
//XXX some archs don't support floppy or sd card
|
||||
public static boolean enableEmulatedFloppy = true;
|
||||
public static boolean enableEmulatedSDCard;
|
||||
public static String destLogFilename = "vectraslog.txt";
|
||||
|
||||
public static String notificationChannelID = "vectras";
|
||||
public static String notificationChannelName = "vectras";
|
||||
public static boolean showToast = false;
|
||||
public static boolean closeFileDescriptors = true;
|
||||
public static String hda_path;
|
||||
public static String extra_params;
|
||||
|
||||
|
||||
public static final String getCacheDir(){
|
||||
return cacheDir.toString();
|
||||
}
|
||||
public static final String getBasefileDir() {
|
||||
return AppConfig.basefiledir;
|
||||
}
|
||||
|
||||
public static String machineFolder = "machines/";
|
||||
public static String getMachineDir(){
|
||||
return getBasefileDir() + machineFolder;
|
||||
}
|
||||
public static String logFilePath = cacheDir + "/vectras/vectras-log.txt";
|
||||
|
||||
|
||||
public static final String defaultDNSServer = "8.8.8.8";
|
||||
public static String state_filename = "vm.state";
|
||||
|
||||
//QMP
|
||||
public static String QMPServer = "127.0.0.1";
|
||||
public static int QMPPort = 4444;
|
||||
|
||||
public static int MAX_DISPLAY_REFRESH_RATE = 100; //Hz
|
||||
|
||||
// VNC Defaults
|
||||
public static String defaultVNCHost = "127.0.0.1";
|
||||
public static final String defaultVNCUsername = "vectras";
|
||||
public static final String defaultVNCPasswd = "";
|
||||
|
||||
//It seems that for new veersion of qemu it expectes a relative number
|
||||
// so we stop using absolute port numbers
|
||||
public static final int defaultVNCPort = 0;
|
||||
|
||||
//Keyboard Layout
|
||||
public static String defaultKeyboardLayout = "en-us";
|
||||
|
||||
|
||||
public static boolean enableToggleKeyboard = false;
|
||||
// Debug
|
||||
public static final boolean debug = true;
|
||||
public static boolean debugQmp = false;
|
||||
|
||||
//remove in production
|
||||
public static boolean debugStrictMode = false;
|
||||
|
||||
public static boolean processMouseHistoricalEvents = false;
|
||||
|
||||
public static String getLocalQMPSocketPath() {
|
||||
return Config.getCacheDir()+"/qmpsocket";
|
||||
}
|
||||
|
||||
public static String getLocalVNCSocketPath() {
|
||||
return Config.getCacheDir()+"/vncsocket";
|
||||
}
|
||||
|
||||
public static enum MouseMode {
|
||||
Trackpad, External
|
||||
}
|
||||
public static MouseMode mouseMode = MouseMode.Trackpad;
|
||||
|
||||
//specify hd interface, alternative we don't need it right now
|
||||
public static boolean enable_hd_if = true;
|
||||
public static String hd_if_type = "ide";
|
||||
|
||||
//Change to true in prod if you want to be notified by default for new versions
|
||||
public static boolean defaultCheckNewVersion = true;
|
||||
|
||||
// App config
|
||||
public static final String datadirpath = VectrasApp.getApp().getExternalFilesDir("data")+"/";
|
||||
public static final String sharedFolder = datadirpath + "Vectras/ProgramFiles/";
|
||||
|
||||
public static String machinename = "VECTRAS";
|
||||
public static int paused = 0;
|
||||
public static String ui = "VNC";
|
||||
public static boolean maxPriority = false;
|
||||
public static final String defaultVNCColorMode = COLORMODEL.C24bit.nameString();
|
||||
public static final ScaleType defaultFullscreenScaleMode = ScaleType.FIT_CENTER;
|
||||
public static final ScaleType defaultScaleModeCenter = ScaleType.CENTER;
|
||||
public static final String defaultInputMode = VncCanvasActivity.TOUCH_ZOOM_MODE;
|
||||
}
|
||||
866
app/src/main/java/com/vectras/qemu/MainSettingsManager.java
Normal file
866
app/src/main/java/com/vectras/qemu/MainSettingsManager.java
Normal file
|
|
@ -0,0 +1,866 @@
|
|||
/*
|
||||
Copyright (C) Max Kastanas 2012
|
||||
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
package com.vectras.qemu;
|
||||
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
|
||||
import com.vectras.vm.R;
|
||||
import com.vectras.vm.SplashActivity;
|
||||
import com.vectras.vm.VectrasApp;
|
||||
|
||||
public class MainSettingsManager extends AppCompatActivity
|
||||
implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||
public static MainSettingsManager activity;
|
||||
|
||||
private static Handler mHandler;
|
||||
public static SharedPreferences sp;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
// TODO: Implement this method
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_settings);
|
||||
|
||||
activity = this;
|
||||
|
||||
sp = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
|
||||
PreferenceFragmentCompat preference = new MainPreferencesFragment();
|
||||
Intent intent = getIntent();
|
||||
|
||||
// add preference settings
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.settingz, preference)
|
||||
.commit();
|
||||
|
||||
// toolbar
|
||||
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(mToolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
|
||||
// Instantiate the new Fragment
|
||||
final Bundle bundle = pref.getExtras();
|
||||
final Fragment fragment = Fragment.instantiate(this, pref.getFragment(), bundle);
|
||||
|
||||
fragment.setTargetFragment(caller, 0);
|
||||
|
||||
// Replace the existing Fragment with the new Fragment
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.settingz, fragment)
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSupportNavigateUp() {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class MainPreferencesFragment extends PreferenceFragmentCompat
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String root_key) {
|
||||
// Load the Preferences from the XML file
|
||||
setPreferencesFromResource(R.xml.headers_preference, root_key);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
if (pref.getKey().equals("app")) {
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class AppPreferencesFragment extends PreferenceFragmentCompat
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String root_key) {
|
||||
// Load the Preferences from the XML file
|
||||
setPreferencesFromResource(R.xml.settings, root_key);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
if (pref.getKey().equals("app")) {
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class UserInterfacePreferencesFragment extends PreferenceFragmentCompat
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mHandler = new Handler();
|
||||
Preference pref = findPreference("modeNight");
|
||||
if (pref != null) {
|
||||
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
onNightMode();
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onNightMode() {
|
||||
if (MainSettingsManager.getModeNight(activity)) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
VectrasApp.getApp().setTheme(R.style.AppTheme);
|
||||
} else {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
VectrasApp.getApp().setTheme(R.style.AppTheme);
|
||||
}
|
||||
activity.finish();
|
||||
startActivity(new Intent(activity, SplashActivity.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String root_key) {
|
||||
// Load the Preferences from the XML file
|
||||
setPreferencesFromResource(R.xml.userinterface, root_key);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class QemuPreferencesFragment extends PreferenceFragmentCompat
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (SDK_INT > 33)
|
||||
findPreference("sharedFolder").setEnabled(false);
|
||||
|
||||
mHandler = new Handler();
|
||||
Preference pref = findPreference("vmArch");
|
||||
if (pref != null) {
|
||||
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
onArch();
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
Preference pref2 = findPreference("kvm");
|
||||
if (pref2 != null) {
|
||||
pref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
onKvm();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void onKvm() {
|
||||
if (getKvm(activity))
|
||||
setMTTCG(activity, true);
|
||||
else
|
||||
setMTTCG(activity, false);
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
activity.finish();
|
||||
startActivity(new Intent(activity, SplashActivity.class));
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
Preference pref3 = findPreference("MTTCG");
|
||||
if (pref3 != null) {
|
||||
pref3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
onMttcg();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void onMttcg() {
|
||||
if (getMTTCG(activity))
|
||||
setKvm(activity, true);
|
||||
else
|
||||
setKvm(activity, false);
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
activity.finish();
|
||||
startActivity(new Intent(activity, SplashActivity.class));
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
/*Preference pref = findPreference("customMemory");
|
||||
if (pref != null) {
|
||||
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
findPreference("memory").setEnabled(!sp.getBoolean("customMemory", false));
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
Preference pref2 = findPreference("customMemory");
|
||||
if (pref2 != null) {
|
||||
pref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference,
|
||||
Object newValue) {
|
||||
if (MainSettingsManager.getVirtio(activity)) {
|
||||
Config.hd_if_type = "virtio";
|
||||
} else {
|
||||
Config.hd_if_type = "ide";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
});
|
||||
}*/
|
||||
}
|
||||
|
||||
private void onMemory() {
|
||||
//findPreference("memory").setEnabled(sp.getBoolean("customMemory", false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
onMemory();
|
||||
if (SDK_INT > 33)
|
||||
findPreference("sharedFolder").setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
onMemory();
|
||||
if (SDK_INT > 33)
|
||||
findPreference("sharedFolder").setEnabled(false);
|
||||
}
|
||||
|
||||
private void onArch() {
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Intent startActivity = new Intent(getContext(), SplashActivity.class);
|
||||
int pendingIntentId = 123456;
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), pendingIntentId, startActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
AlarmManager mgr = (AlarmManager) MainSettingsManager.activity.getSystemService(Context.ALARM_SERVICE);
|
||||
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pendingIntent);
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String root_key) {
|
||||
// Load the Preferences from the XML file
|
||||
setPreferencesFromResource(R.xml.qemu, root_key);
|
||||
if (SDK_INT > 33)
|
||||
findPreference("sharedFolder").setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class VncPreferencesFragment extends PreferenceFragmentCompat
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle bundle, String root_key) {
|
||||
// Load the Preferences from the XML file
|
||||
setPreferencesFromResource(R.xml.vnc, root_key);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
if (pref.getKey().equals("app")) {
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean getVncExternal(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("vncExternal", false);
|
||||
}
|
||||
|
||||
public static void setVncExternal(Activity activity, boolean vncExternal) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("vncExternal", vncExternal);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static int getOrientationSetting(Activity activity) {
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
int orientation = prefs.getInt("orientation", 0);
|
||||
// UIUtils.log("Getting First time: " + firstTime);
|
||||
return orientation;
|
||||
}
|
||||
|
||||
public static void setOrientationSetting(Activity activity, int orientation) {
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putInt("orientation", orientation);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
static boolean getPrio(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("HighPrio", false);
|
||||
}
|
||||
|
||||
public static void setPrio(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("HighPrio", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
public static boolean getAlwaysShowMenuToolbar(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("AlwaysShowMenuToolbar", false);
|
||||
}
|
||||
|
||||
public static void setAlwaysShowMenuToolbar(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("AlwaysShowMenuToolbar", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
public static boolean getFullscreen(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("ShowFullscreen", true);
|
||||
}
|
||||
|
||||
public static void setFullscreen(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("ShowFullscreen", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
public static boolean getDesktopMode(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("DesktopMode", false);
|
||||
}
|
||||
|
||||
public static void setDesktopMode(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("DesktopMode", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
public static boolean getEnableLegacyFileManager(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("EnableLegacyFileManager", false);
|
||||
}
|
||||
|
||||
|
||||
public static void setEnableLegacyFileManager(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("EnableLegacyFileManager", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
public static String getLastDir(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String imagesDir = prefs.getString("lastDir", null);
|
||||
return imagesDir;
|
||||
}
|
||||
|
||||
public static void setLastDir(Context context, String imagesPath) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("lastDir", imagesPath);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static String getImagesDir(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String imagesDir = prefs.getString("imagesDir", null);
|
||||
return imagesDir;
|
||||
}
|
||||
|
||||
public static void setImagesDir(Context context, String imagesPath) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("imagesDir", imagesPath);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
|
||||
public static String getExportDir(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String imagesDir = prefs.getString("exportDir", null);
|
||||
return imagesDir;
|
||||
}
|
||||
|
||||
public static void setExportDir(Context context, String imagesPath) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("exportDir", imagesPath);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
|
||||
public static String getSharedDir(Context context) {
|
||||
String lastDir = Environment.getExternalStorageDirectory().getPath();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return prefs.getString("sharedDir", lastDir);
|
||||
}
|
||||
|
||||
public static void setSharedDir(Context context, String lastDir) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("sharedDir", lastDir);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
|
||||
public static Boolean getMTTCG(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
Boolean MTTCG = prefs.getBoolean("MTTCG", true);
|
||||
return MTTCG;
|
||||
}
|
||||
|
||||
public static void setMTTCG(Context context, Boolean MTTCG) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("MTTCG", MTTCG);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static int getCpuCores(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int cpuCores = prefs.getInt("cpuCores", 1);
|
||||
return cpuCores;
|
||||
}
|
||||
|
||||
public static void setCpuCores(Context context, int cpuCores) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putInt("cpuCores", cpuCores);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static int getExitCode(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int exitCode = prefs.getInt("exitCode", 1);
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
public static void setExitCode(Context context, int exitCode) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putInt("exitCode", exitCode);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static int getCpuNum(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int cpuNum = Integer.parseInt(prefs.getString("cpuNum", "1"));
|
||||
return cpuNum;
|
||||
}
|
||||
|
||||
public static void setCpuNum(Context context, String cpuNum) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("cpuNum", cpuNum);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static String getControlMode(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String controlMode = prefs.getString("controlMode", "D");
|
||||
return controlMode;
|
||||
}
|
||||
|
||||
public static void setControlMode(Context context, String controlMode) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("controlMode", controlMode);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
|
||||
public static void setModeNight(Context context, Boolean nightMode) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("modeNight", nightMode);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
public static Boolean getModeNight(Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return prefs.getBoolean("modeNight", false);
|
||||
}
|
||||
|
||||
public static void setCusRam(Activity activity, Boolean cusRam) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("customMemory", cusRam);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getCusRam(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("customMemory", false);
|
||||
}
|
||||
|
||||
public static void setVirtio(Activity activity, Boolean virtio) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("virtio", virtio);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getVirtio(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("virtio", false);
|
||||
}
|
||||
|
||||
public static void setAvx(Activity activity, boolean AVX) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("AVX", AVX);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getAvx(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("AVX", false);
|
||||
}
|
||||
|
||||
public static void setTbSize(Activity activity, String TbSize) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("TbSize", TbSize);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getTbSize(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("TbSize", "2048");
|
||||
}
|
||||
|
||||
public static void setBoot(Activity activity, String boot) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("boot", boot);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getBoot(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("boot", "c");
|
||||
}
|
||||
|
||||
|
||||
public static void setCpu(Activity activity, String cpu) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("cpu", cpu);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getCpu(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("cpu", "qemu64");
|
||||
}
|
||||
|
||||
|
||||
public static void setVmUi(Activity activity, String vmUi) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("vmUi", vmUi);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getVmUi(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("vmUi", "VNC");
|
||||
}
|
||||
public static void setSoundCard(Activity activity, String soundCard) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("soundCard", soundCard);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getSoundCard(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("soundCard", "None");
|
||||
}
|
||||
|
||||
public static void setUsbTablet(Activity activity, boolean UsbTablet) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("UsbTablet", UsbTablet);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getUsbTablet(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("UsbTablet", false);
|
||||
}
|
||||
|
||||
public static void setCustomParams(Activity activity, String customParams) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("customParams", customParams);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getCustomParams(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("customParams", "");
|
||||
}
|
||||
|
||||
public static void setSharedFolder(Activity activity, boolean customParams) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("customParams", customParams);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getSharedFolder(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("sharedFolder", false);
|
||||
}
|
||||
|
||||
public static void setArch(Activity activity, String vmArch) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putString("vmArch", vmArch);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static String getArch(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getString("vmArch", "X86_64");
|
||||
}
|
||||
|
||||
public static void setKvm(Activity activity, boolean kvm) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("kvm", kvm);
|
||||
edit.apply();
|
||||
}
|
||||
|
||||
public static boolean getKvm(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("kvm", false);
|
||||
}
|
||||
|
||||
public static boolean isFirstLaunch(Activity activity) {
|
||||
PackageInfo pInfo = null;
|
||||
|
||||
try {
|
||||
pInfo = activity.getPackageManager().getPackageInfo(activity.getClass().getPackage().getName(),
|
||||
PackageManager.GET_META_DATA);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
boolean firstTime = prefs.getBoolean("firstTime" + pInfo.versionName, true);
|
||||
return firstTime;
|
||||
}
|
||||
|
||||
public static void setFirstLaunch(Activity activity) {
|
||||
PackageInfo pInfo = null;
|
||||
|
||||
try {
|
||||
pInfo = activity.getPackageManager().getPackageInfo(activity.getClass().getPackage().getName(),
|
||||
PackageManager.GET_META_DATA);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("firstTime" + pInfo.versionName, false);
|
||||
edit.commit();
|
||||
}
|
||||
|
||||
|
||||
public static boolean getPromptUpdateVersion(Activity activity) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
return prefs.getBoolean("updateVersionPrompt", Config.defaultCheckNewVersion);
|
||||
}
|
||||
|
||||
|
||||
public static void setPromptUpdateVersion(Activity activity, boolean flag) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
SharedPreferences.Editor edit = prefs.edit();
|
||||
edit.putBoolean("updateVersionPrompt", flag);
|
||||
edit.apply();
|
||||
// UIUtils.log("Setting First time: ");
|
||||
}
|
||||
|
||||
}
|
||||
1278
app/src/main/java/com/vectras/qemu/MainVNCActivity.java
Normal file
1278
app/src/main/java/com/vectras/qemu/MainVNCActivity.java
Normal file
File diff suppressed because it is too large
Load diff
93
app/src/main/java/com/vectras/qemu/SettingsFragment.java
Normal file
93
app/src/main/java/com/vectras/qemu/SettingsFragment.java
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package com.vectras.qemu;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
|
||||
import com.vectras.vm.R;
|
||||
import com.vectras.vm.SplashActivity;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragmentCompat {
|
||||
|
||||
private Handler mHandler;
|
||||
public SharedPreferences mPref;
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
|
||||
setPreferencesFromResource(R.xml.settings, rootKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mHandler = new Handler();
|
||||
SharedPreferences.OnSharedPreferenceChangeListener listener;
|
||||
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
|
||||
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
|
||||
switch (key) {
|
||||
|
||||
case "modeNight":
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Intent startActivity = new Intent(getContext(), SplashActivity.class);
|
||||
int pendingIntentId = 123456;
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), pendingIntentId, startActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
|
||||
AlarmManager mgr = (AlarmManager) MainSettingsManager.activity.getSystemService(Context.ALARM_SERVICE);
|
||||
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pendingIntent);
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}, 300);
|
||||
|
||||
getActivity().finish();
|
||||
break;
|
||||
case "customMemory":
|
||||
if (prefs.getBoolean("customMemory", false))
|
||||
findPreference("memory").setEnabled(true);
|
||||
else
|
||||
findPreference("memory").setEnabled(false);
|
||||
break;
|
||||
case "MTTCG":
|
||||
if (prefs.getBoolean("MTTCG", false)) {
|
||||
findPreference("cpuNum").setEnabled(false);
|
||||
MainSettingsManager.setCpuCores(getContext(), 1);
|
||||
} else {
|
||||
findPreference("cpuNum").setEnabled(true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mPref = getPreferenceManager().getDefaultSharedPreferences(getContext());
|
||||
if (mPref != null) {
|
||||
mPref.registerOnSharedPreferenceChangeListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
if (mPref.getBoolean("customMemory", false))
|
||||
findPreference("memory").setEnabled(true);
|
||||
else
|
||||
findPreference("memory").setEnabled(false);
|
||||
|
||||
if (mPref.getBoolean("MTTCG", false)) {
|
||||
findPreference("cpuNum").setEnabled(false);
|
||||
} else {
|
||||
findPreference("cpuNum").setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
271
app/src/main/java/com/vectras/qemu/utils/FileInstaller.java
Normal file
271
app/src/main/java/com/vectras/qemu/utils/FileInstaller.java
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
Copyright (C) Max Kastanas 2012
|
||||
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
package com.vectras.qemu.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.AssetManager;
|
||||
import android.net.Uri;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import android.util.Log;
|
||||
|
||||
import com.vectras.qemu.Config;
|
||||
import com.vectras.vm.utils.UIUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author dev
|
||||
*/
|
||||
public class FileInstaller {
|
||||
|
||||
public static void installFiles(Activity activity, boolean force) {
|
||||
|
||||
Log.v("Installer", "Installing files...");
|
||||
File tmpDir = new File(Config.getBasefileDir());
|
||||
if (!tmpDir.exists()) {
|
||||
tmpDir.mkdirs();
|
||||
}
|
||||
|
||||
File tmpDir1 = new File(Config.getMachineDir());
|
||||
if (!tmpDir1.exists()) {
|
||||
tmpDir1.mkdirs();
|
||||
}
|
||||
|
||||
|
||||
//Install base dir
|
||||
File dir = new File(Config.getBasefileDir());
|
||||
if (dir.exists() && dir.isDirectory()) {
|
||||
//don't create again
|
||||
} else if (dir.exists() && !dir.isDirectory()) {
|
||||
Log.v("Installer", "Could not create Dir, file found: " + Config.getBasefileDir());
|
||||
return;
|
||||
} else if (!dir.exists()) {
|
||||
dir.mkdir();
|
||||
}
|
||||
|
||||
String destDir = Config.getBasefileDir();
|
||||
|
||||
//Get each file in assets under ./roms/ and install in SDCARD
|
||||
AssetManager am = activity.getResources().getAssets();
|
||||
String[] files = null;
|
||||
try {
|
||||
files = am.list("roms");
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(FileInstaller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
Log.v("Installer", "Could not install files: " + ex.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
//Log.v("Installer", "File: " + files[i]);
|
||||
String[] subfiles = null;
|
||||
try {
|
||||
subfiles = am.list("roms/" + files[i]);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(FileInstaller.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
if (subfiles != null && subfiles.length > 0) {
|
||||
//Install base dir
|
||||
File dir1 = new File(Config.getBasefileDir() + files[i]);
|
||||
if (dir1.exists() && dir1.isDirectory()) {
|
||||
//don't create again
|
||||
} else if (dir1.exists() && !dir1.isDirectory()) {
|
||||
Log.v("Installer", "Could not create Dir, file found: " + Config.getBasefileDir() + files[i]);
|
||||
return;
|
||||
} else if (!dir1.exists()) {
|
||||
dir1.mkdir();
|
||||
}
|
||||
for (int k = 0; k < subfiles.length; k++) {
|
||||
|
||||
File file = new File(destDir, files[i] + "/" + subfiles[k]);
|
||||
if(!file.exists() || force) {
|
||||
Log.v("Installer", "Installing file: " + file.getPath());
|
||||
installAssetFile(activity, files[i] + "/" + subfiles[k], destDir, "roms", null);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
File file = new File(destDir, files[i]);
|
||||
if(!file.exists() || force) {
|
||||
Log.v("Installer", "Installing file: " + file.getPath());
|
||||
installAssetFile(activity, files[i], Config.getBasefileDir(), "roms", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
// InputStream is = am.open(srcFile);
|
||||
|
||||
}
|
||||
|
||||
public static boolean installAssetFile(Activity activity, String srcFile,
|
||||
String destDir, String assetsDir, String destFile) {
|
||||
try {
|
||||
AssetManager am = activity.getResources().getAssets(); // get the local asset manager
|
||||
InputStream is = am.open(assetsDir + "/" + srcFile); // open the input stream for reading
|
||||
File destDirF = new File(destDir);
|
||||
if (!destDirF.exists()) {
|
||||
boolean res = destDirF.mkdirs();
|
||||
if(!res){
|
||||
UIUtils.toastShort(activity, "Could not create directory for image");
|
||||
}
|
||||
}
|
||||
|
||||
if(destFile==null)
|
||||
destFile=srcFile;
|
||||
OutputStream os = new FileOutputStream(destDir + "/" + destFile);
|
||||
byte[] buf = new byte[8092];
|
||||
int n;
|
||||
while ((n = is.read(buf)) > 0) {
|
||||
os.write(buf, 0, n);
|
||||
}
|
||||
os.close();
|
||||
is.close();
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
Log.e("Installer", "failed to install file: " + destFile + ", Error:" + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Uri installImageTemplateToSDCard(Activity activity, String srcFile,
|
||||
Uri destDir, String assetsDir, String destFile) {
|
||||
|
||||
DocumentFile destFileF = null;
|
||||
OutputStream os = null;
|
||||
InputStream is = null;
|
||||
Uri uri = null;
|
||||
|
||||
try {
|
||||
|
||||
DocumentFile dir = DocumentFile.fromTreeUri(activity, destDir);
|
||||
AssetManager am = activity.getResources().getAssets(); // get the local asset manager
|
||||
is = am.open(assetsDir + "/" + srcFile); // open the input stream for reading
|
||||
|
||||
if(destFile==null)
|
||||
destFile=srcFile;
|
||||
|
||||
//Create the file if doesn't exist
|
||||
destFileF = dir.findFile(destFile);
|
||||
if(destFileF == null) {
|
||||
destFileF = dir.createFile("application/octet-stream", destFile);
|
||||
}
|
||||
else {
|
||||
UIUtils.toastShort(activity, "File exists, choose another filename");
|
||||
return null;
|
||||
}
|
||||
|
||||
//Write to the dest
|
||||
os = activity.getContentResolver().openOutputStream(destFileF.getUri());
|
||||
//OutputStream os = new FileOutputStream(destDir + "/" + destFile);
|
||||
byte[] buf = new byte[8092];
|
||||
int n;
|
||||
while ((n = is.read(buf)) > 0) {
|
||||
os.write(buf, 0, n);
|
||||
}
|
||||
|
||||
//success
|
||||
uri = destFileF.getUri();
|
||||
|
||||
} catch (Exception ex) {
|
||||
Log.e("Installer", "failed to install file: " + destFile + ", Error:" + ex.getMessage());
|
||||
} finally {
|
||||
if(os!=null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if(is!=null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
||||
public static String installImageTemplateToExternalStorage(Activity activity, String srcFile,
|
||||
String destDir, String assetsDir, String destFile) {
|
||||
|
||||
File file = new File(destDir, destFile);
|
||||
String filePath = null;
|
||||
OutputStream os = null;
|
||||
InputStream is = null;
|
||||
try {
|
||||
|
||||
AssetManager am = activity.getResources().getAssets(); // get the local asset manager
|
||||
is = am.open(assetsDir + "/" + srcFile); // open the input stream for reading
|
||||
|
||||
if(destFile==null)
|
||||
destFile=srcFile;
|
||||
|
||||
//Create the file if doesn't exist
|
||||
if(!file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
else {
|
||||
UIUtils.toastShort(activity, "File exists, choose another filename");
|
||||
return null;
|
||||
}
|
||||
|
||||
//Write to the dest
|
||||
os = new FileOutputStream(file);
|
||||
|
||||
//OutputStream os = new FileOutputStream(destDir + "/" + destFile);
|
||||
byte[] buf = new byte[8092];
|
||||
int n;
|
||||
while ((n = is.read(buf)) > 0) {
|
||||
os.write(buf, 0, n);
|
||||
}
|
||||
|
||||
//success
|
||||
filePath = file.getAbsolutePath();
|
||||
|
||||
} catch (Exception ex) {
|
||||
Log.e("Installer", "failed to install file: " + destFile + ", Error:" + ex.getMessage());
|
||||
} finally {
|
||||
if(os!=null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if(is!=null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
}
|
||||
647
app/src/main/java/com/vectras/qemu/utils/FileUtils.java
Normal file
647
app/src/main/java/com/vectras/qemu/utils/FileUtils.java
Normal file
|
|
@ -0,0 +1,647 @@
|
|||
/*
|
||||
Copyright (C) Max Kastanas 2012
|
||||
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
package com.vectras.qemu.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import android.text.Spannable;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.vectras.vm.R;
|
||||
import com.vectras.qemu.Config;
|
||||
import com.vectras.vm.utils.UIUtils;
|
||||
|
||||
/**
|
||||
* @author dev
|
||||
*/
|
||||
public class FileUtils {
|
||||
private final static String TAG = "FileUtils";
|
||||
public static HashMap<Integer, FileInfo> fds = new HashMap<Integer, FileInfo>();
|
||||
|
||||
public static String getNativeLibDir(Context context) {
|
||||
return context.getApplicationInfo().nativeLibraryDir;
|
||||
}
|
||||
|
||||
public static String getFullPathFromDocumentFilePath(String filePath) {
|
||||
|
||||
filePath = filePath.replaceAll("%3A", "^3A");
|
||||
int index = filePath.lastIndexOf("^3A");
|
||||
if (index > 0)
|
||||
filePath = filePath.substring(index + 3);
|
||||
if (!filePath.startsWith("/"))
|
||||
filePath = "/" + filePath;
|
||||
|
||||
// filePath = filePath.replaceAll("%2F", "/");
|
||||
// filePath = filePath.replaceAll("\\^2F", "/");
|
||||
|
||||
//remove any spaces encoded by the ASF
|
||||
try {
|
||||
filePath = URLDecoder.decode(filePath, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static String getFilenameFromPath(String filePath) {
|
||||
filePath = filePath.replaceAll("%2F", "/");
|
||||
filePath = filePath.replaceAll("%3A", "/");
|
||||
filePath = filePath.replaceAll("\\^2F", "/");
|
||||
filePath = filePath.replaceAll("\\^3A", "/");
|
||||
|
||||
|
||||
int index = filePath.lastIndexOf("/");
|
||||
if (index > 0)
|
||||
return filePath.substring(index + 1);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static String unconvertDocumentFilePath(String filePath) {
|
||||
if (filePath != null && filePath.startsWith("/content//")) {
|
||||
filePath = filePath.replace("/content//", "content://");
|
||||
filePath = filePath.replaceAll("\\^\\^\\^", "%");
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static String convertDocumentFilePath(String filePath) {
|
||||
if (filePath != null && filePath.startsWith("content://")) {
|
||||
filePath = filePath.replace("content://", "/content//");
|
||||
filePath = filePath.replaceAll("%", "\\^\\^\\^");;
|
||||
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static void saveFileContents(String filePath, String contents) {
|
||||
// TODO: we assume that the contents are of small size so we keep in an array
|
||||
byteArrayToFile(contents.getBytes(), new File(filePath));
|
||||
}
|
||||
|
||||
public static void byteArrayToFile(byte[] byteData, File filePath) {
|
||||
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(filePath);
|
||||
fos.write(byteData);
|
||||
fos.close();
|
||||
|
||||
} catch (FileNotFoundException ex) {
|
||||
System.out.println("FileNotFoundException : " + ex);
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("IOException : " + ioe);
|
||||
}
|
||||
|
||||
}
|
||||
public static InputStream getStreamFromFilePath(Context context, String importFilePath) throws FileNotFoundException {
|
||||
InputStream stream = null;
|
||||
if (importFilePath.startsWith("content://")) {
|
||||
Uri uri = Uri.parse(importFilePath);
|
||||
String mode = "rw";
|
||||
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, mode);
|
||||
return new FileInputStream(pfd.getFileDescriptor());
|
||||
} else {
|
||||
return new FileInputStream(importFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFileContents(String filePath) {
|
||||
|
||||
File file = new File(filePath);
|
||||
if(!file.exists())
|
||||
return "";
|
||||
StringBuilder builder = new StringBuilder("");
|
||||
try {
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
byte[] buff = new byte[32768];
|
||||
int bytesRead = 0;
|
||||
while ((bytesRead = stream.read(buff, 0, buff.length)) > 0) {
|
||||
builder.append(new String(buff, "UTF-8"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String contents = builder.toString();
|
||||
return contents;
|
||||
}
|
||||
|
||||
public static void viewVectrasLog(final Activity activity) {
|
||||
|
||||
String contents = FileUtils.getFileContents(Config.logFilePath);
|
||||
|
||||
if (contents.length() > 50 * 1024)
|
||||
contents = contents.substring(0, 25 * 1024)
|
||||
+ "\n.....\n" +
|
||||
contents.substring(contents.length() - 25 * 1024);
|
||||
|
||||
final String finalContents = contents;
|
||||
final Spannable contentsFormatted = UIUtils.formatAndroidLog(contents);
|
||||
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (Config.viewLogInternally) {
|
||||
UIUtils.UIAlertLog(activity, "Vectras Log", contentsFormatted);
|
||||
} else {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_EDIT);
|
||||
File file = new File(Config.logFilePath);
|
||||
Uri uri = Uri.fromFile(file);
|
||||
intent.setDataAndType(uri, "text/plain");
|
||||
activity.startActivity(intent);
|
||||
} catch (Exception ex) {
|
||||
// UIUtils.toastShort(activity, "Could not find a Text Viewer on your device");
|
||||
UIUtils.UIAlertLog(activity, "Vectras Log", contentsFormatted);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public static boolean fileValid(Context context, String path) {
|
||||
|
||||
if (path == null || path.equals(""))
|
||||
return true;
|
||||
if (path.startsWith("content://") || path.startsWith("/content/")) {
|
||||
int fd = get_fd(context, path);
|
||||
if (fd <= 0)
|
||||
return false;
|
||||
} else {
|
||||
File file = new File(path);
|
||||
return file.exists();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//TODO: we should pass the modes from the backend and translate them
|
||||
// instead of blindly using "rw". ie ISOs should be read only.
|
||||
public static int get_fd(final Context context, String path) {
|
||||
synchronized (fds) {
|
||||
int fd = 0;
|
||||
if (path == null)
|
||||
return 0;
|
||||
|
||||
// Log.d(TAG, "Opening Filepath: " + path);
|
||||
if (path.startsWith("/content//") || path.startsWith("content://")) {
|
||||
String npath = unconvertDocumentFilePath(path);
|
||||
|
||||
//Is this needed?
|
||||
// FileInfo info = getExistingFd(npath);
|
||||
// if (info!=null) {
|
||||
// ParcelFileDescriptor pfd = info.pfd;
|
||||
// fd = pfd.getFd();
|
||||
// Log.d(TAG, "Retrieved hashed documentfile: " + npath + ", FD: " + fd);
|
||||
// return fd;
|
||||
// }
|
||||
|
||||
|
||||
// Log.d(TAG, "Opening unconverted: " + npath);
|
||||
try {
|
||||
Uri uri = Uri.parse(npath);
|
||||
String mode = "rw";
|
||||
if (path.toLowerCase().endsWith(".iso"))
|
||||
mode = "r";
|
||||
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, mode);
|
||||
fd = pfd.getFd();
|
||||
// Log.d(TAG, "Opening DocumentFile: " + npath + ", FD: " + fd);
|
||||
fds.put(fd, new FileInfo(path, npath, pfd));
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Could not open DocumentFile: " + npath + ", FD: " + fd);
|
||||
if(Config.debug)
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
//Is this needed?
|
||||
// FileInfo info = getExistingFd(path);
|
||||
// if (info!=null) {
|
||||
// ParcelFileDescriptor pfd = info.pfd;
|
||||
// fd = pfd.getFd();
|
||||
// Log.d(TAG, "Retrieved hashed file: " + path + ", FD: " + fd);
|
||||
// return fd;
|
||||
// }
|
||||
|
||||
try {
|
||||
int mode = ParcelFileDescriptor.MODE_READ_WRITE;
|
||||
if (path.toLowerCase().endsWith(".iso"))
|
||||
mode = ParcelFileDescriptor.MODE_READ_ONLY;
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.exists())
|
||||
file.createNewFile();
|
||||
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, mode);
|
||||
fd = pfd.getFd();
|
||||
fds.put(fd, new FileInfo(path, path, pfd));
|
||||
Log.d(TAG, "Opening File: " + path + ", FD: " + fd);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Could not open File: " + path + ", FD: " + fd);
|
||||
if(Config.debug)
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
|
||||
private static FileInfo getExistingFd(String npath) {
|
||||
Set<Map.Entry<Integer, FileInfo>> fileInfoSet = fds.entrySet();
|
||||
Iterator<Map.Entry<Integer, FileInfo>> iter = fileInfoSet.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<Integer, FileInfo> entry = iter.next();
|
||||
FileInfo fileInfo = entry.getValue();
|
||||
if (fileInfo.npath.equals(npath)) {
|
||||
return fileInfo;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void close_fds() {
|
||||
synchronized (fds) {
|
||||
Integer[] fds = FileUtils.fds.keySet().toArray(new Integer[FileUtils.fds.keySet().size()]);
|
||||
for (int i = 0; i < fds.length; i++) {
|
||||
FileUtils.close_fd(fds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int close_fd(int fd) {
|
||||
if(!Config.closeFileDescriptors) {
|
||||
return 0;
|
||||
}
|
||||
synchronized (fds) {
|
||||
// Log.d(TAG, "Closing FD: " + fd);
|
||||
if (FileUtils.fds.containsKey(fd)) {
|
||||
|
||||
FileInfo info = FileUtils.fds.get(fd);
|
||||
|
||||
|
||||
try {
|
||||
|
||||
ParcelFileDescriptor pfd = info.pfd;
|
||||
try {
|
||||
pfd.getFileDescriptor().sync();
|
||||
} catch (IOException e) {
|
||||
if(Config.debug) {
|
||||
Log.w(TAG, "Syncing DocumentFile: " + info.path + ": " + fd + " : " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Log.d(TAG, "Closing DocumentFile: " + info.npath + ", FD: " + fd);
|
||||
pfd.close();
|
||||
FileUtils.fds.remove(fd);
|
||||
return 0; // success for Native side
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Error Closing DocumentFile: " + info.path + ": " + fd + " : " + e);
|
||||
if(Config.debug)
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
ParcelFileDescriptor pfd = null;
|
||||
String path = "unknown";
|
||||
try {
|
||||
|
||||
//xxx: check the hash
|
||||
FileInfo info = FileUtils.fds.get(fd);
|
||||
if(info!=null) {
|
||||
pfd = info.pfd;
|
||||
path = info.path;
|
||||
// Log.d(TAG, "Closing hashe File FD: " + fd + ": " + info.path);
|
||||
}
|
||||
|
||||
//xxx: else get a new parcel
|
||||
if(pfd == null)
|
||||
pfd = ParcelFileDescriptor.fromFd(fd);
|
||||
|
||||
// Log.d(TAG, "Closing File FD: " + fd);
|
||||
try {
|
||||
pfd.getFileDescriptor().sync();
|
||||
} catch (IOException e) {
|
||||
if(Config.debug) {
|
||||
Log.e(TAG, "Error Syncing File: " + path + ": " + fd + " : " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
pfd.close();
|
||||
return 0;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error Closing File FD: " + path + ": " + fd + " : " + e);
|
||||
if(Config.debug)
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void startLogging() {
|
||||
|
||||
if (Config.logFilePath == null) {
|
||||
Log.e(TAG, "Log file is not setup");
|
||||
return;
|
||||
}
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
|
||||
FileOutputStream os = null;
|
||||
File logFile = null;
|
||||
try {
|
||||
logFile = new File(Config.logFilePath);
|
||||
if (logFile.exists()) {
|
||||
logFile.delete();
|
||||
}
|
||||
logFile.createNewFile();
|
||||
Runtime.getRuntime().exec("logcat -c");
|
||||
Process process = Runtime.getRuntime().exec("logcat v main");
|
||||
os = new FileOutputStream(logFile);
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
|
||||
StringBuilder log = new StringBuilder("");
|
||||
String line = "";
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
log.setLength(0);
|
||||
log.append(line).append("\n");
|
||||
os.write(log.toString().getBytes("UTF-8"));
|
||||
os.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (os != null) {
|
||||
os.flush();
|
||||
os.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
t.setName("VectrasLogger");
|
||||
t.start();
|
||||
}
|
||||
|
||||
public static String LoadFile(Activity activity, String fileName, boolean loadFromRawFolder) throws IOException {
|
||||
// Create a InputStream to read the file into
|
||||
InputStream iS;
|
||||
if (loadFromRawFolder) {
|
||||
// get the resource id from the file name
|
||||
int rID = activity.getResources().getIdentifier(activity.getClass().getPackage().getName() + ":raw/" + fileName,
|
||||
null, null);
|
||||
// get the file as a stream
|
||||
iS = activity.getResources().openRawResource(rID);
|
||||
} else {
|
||||
// get the file as a stream
|
||||
iS = activity.getResources().getAssets().open(fileName);
|
||||
}
|
||||
|
||||
ByteArrayOutputStream oS = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[iS.available()];
|
||||
int bytesRead = 0;
|
||||
while ((bytesRead = iS.read(buffer)) > 0) {
|
||||
oS.write(buffer);
|
||||
}
|
||||
oS.close();
|
||||
iS.close();
|
||||
|
||||
// return the output stream as a String
|
||||
return oS.toString();
|
||||
}
|
||||
|
||||
public static String getExtensionFromFilename(String fileName) {
|
||||
if (fileName == null)
|
||||
return "";
|
||||
|
||||
int index = fileName.lastIndexOf(".");
|
||||
if (index >= 0) {
|
||||
return fileName.substring(index + 1);
|
||||
} else
|
||||
return "";
|
||||
}
|
||||
|
||||
public static int getIconForFile(String file) {
|
||||
// file = file.toLowerCase();
|
||||
// String ext = FileUtils.getExtensionFromFilename(file).toLowerCase();
|
||||
//
|
||||
// if(ext.equals("img") || ext.equals("qcow")
|
||||
// || ext.equals("qcow2") || ext.equals("vmdk") || ext.equals("vdi") || ext.equals("cow")
|
||||
// || ext.equals("dmg") || ext.equals("bochs") || ext.equals("vpc")
|
||||
// || ext.equals("vhd") || ext.equals("fs"))
|
||||
// return R.drawable.harddisk;
|
||||
// else if(ext.equals("iso"))
|
||||
// return R.drawable.cd;
|
||||
// else if(ext.equals("ima"))
|
||||
// return R.drawable.floppy;
|
||||
// else if(ext.equals("csv"))
|
||||
// return R.drawable.importvms;
|
||||
// else if(file.contains("kernel") || file.contains("vmlinuz") || file.contains("initrd"))
|
||||
// return R.drawable.sysfile;
|
||||
// else
|
||||
// return R.drawable.close;
|
||||
return R.mipmap.ic_launcher;
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static Uri saveLogFileSDCard(Activity activity,
|
||||
Uri destDir) {
|
||||
|
||||
DocumentFile destFileF = null;
|
||||
OutputStream os = null;
|
||||
Uri uri = null;
|
||||
|
||||
try {
|
||||
String logFileContents = getFileContents(Config.logFilePath);
|
||||
DocumentFile dir = DocumentFile.fromTreeUri(activity, destDir);
|
||||
|
||||
//Create the file if doesn't exist
|
||||
destFileF = dir.findFile(Config.destLogFilename);
|
||||
if(destFileF == null) {
|
||||
destFileF = dir.createFile(MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt"), Config.destLogFilename);
|
||||
}
|
||||
|
||||
//Write to the dest
|
||||
os = activity.getContentResolver().openOutputStream(destFileF.getUri());
|
||||
os.write(logFileContents.getBytes());
|
||||
|
||||
//success
|
||||
uri = destFileF.getUri();
|
||||
|
||||
} catch (Exception ex) {
|
||||
UIUtils.toastShort(activity, "Failed to save log file: " + ex.getMessage());
|
||||
} finally {
|
||||
if(os!=null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
public static String saveLogFileLegacy(Activity activity,
|
||||
String destLogFilePath) {
|
||||
|
||||
String filePath = null;
|
||||
File destFileF = new File(destLogFilePath, Config.destLogFilename);
|
||||
|
||||
try {
|
||||
String logFileContents = getFileContents(Config.logFilePath);
|
||||
FileUtils.saveFileContents(destFileF.getAbsolutePath(), logFileContents);
|
||||
|
||||
//success
|
||||
filePath = destFileF.getAbsolutePath();
|
||||
|
||||
} catch (Exception ex) {
|
||||
UIUtils.toastShort(activity, "Failed to save log file: " + destFileF.getAbsolutePath() + ", Error:" + ex.getMessage());
|
||||
} finally {
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
|
||||
public static String getFileUriFromIntent(Activity activity, Intent data, boolean write) {
|
||||
if(data == null)
|
||||
return null;
|
||||
|
||||
Uri uri = data.getData();
|
||||
DocumentFile pickedFile = DocumentFile.fromSingleUri(activity, uri);
|
||||
String file = uri.toString();
|
||||
if (!file.contains("com.android.externalstorage.documents")) {
|
||||
UIUtils.showFileNotSupported(activity);
|
||||
return null;
|
||||
}
|
||||
activity.grantUriPermission(activity.getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
if(write)
|
||||
activity.grantUriPermission(activity.getPackageName(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
activity.grantUriPermission(activity.getPackageName(), uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
||||
|
||||
int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
||||
if(write)
|
||||
takeFlags = takeFlags | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
||||
|
||||
activity.getContentResolver().takePersistableUriPermission(uri, takeFlags);
|
||||
return file;
|
||||
}
|
||||
|
||||
public static String getDirPathFromIntent(Activity activity, Intent data) {
|
||||
if(data == null)
|
||||
return null;
|
||||
Bundle b = data.getExtras();
|
||||
String file = b.getString("currDir");
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
public static String getFilePathFromIntent(Activity activity, Intent data) {
|
||||
if(data == null)
|
||||
return null;
|
||||
Bundle b = data.getExtras();
|
||||
String file = b.getString("file");
|
||||
return file;
|
||||
}
|
||||
|
||||
public static class FileInfo {
|
||||
public String path;
|
||||
public String npath;
|
||||
public ParcelFileDescriptor pfd;
|
||||
|
||||
public FileInfo(String path, String npath, ParcelFileDescriptor pfd) {
|
||||
this.npath = npath;
|
||||
this.path = path;
|
||||
this.pfd = pfd;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void saveLogToFile(final Activity activity, final String logFileDestDir) {
|
||||
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
|
||||
String displayName = null;
|
||||
if (logFileDestDir.startsWith("content://")) {
|
||||
Uri exportDirUri = Uri.parse(logFileDestDir);
|
||||
Uri fileCreatedUri = FileUtils.saveLogFileSDCard(activity,
|
||||
exportDirUri);
|
||||
displayName = FileUtils.getFullPathFromDocumentFilePath(fileCreatedUri.toString());
|
||||
} else {
|
||||
String filePath = FileUtils.saveLogFileLegacy(activity, logFileDestDir);
|
||||
displayName = filePath;
|
||||
}
|
||||
|
||||
if(displayName!=null){
|
||||
|
||||
UIUtils.toastShort(activity, "Logfile saved");
|
||||
}
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
277
app/src/main/java/com/vectras/qemu/utils/QmpClient.java
Normal file
277
app/src/main/java/com/vectras/qemu/utils/QmpClient.java
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
package com.vectras.qemu.utils;
|
||||
|
||||
import android.net.LocalSocket;
|
||||
import android.net.LocalSocketAddress;
|
||||
import android.util.Log;
|
||||
|
||||
import com.vectras.qemu.Config;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class QmpClient {
|
||||
|
||||
private static final String TAG = "QmpClient";
|
||||
private static String requestCommandMode = "{ \"execute\": \"qmp_capabilities\" }";
|
||||
public static boolean allow_external = false;
|
||||
|
||||
public synchronized static String sendCommand(String command) {
|
||||
String response = null;
|
||||
int trial=0;
|
||||
Socket pingSocket = null;
|
||||
LocalSocket localSocket = null;
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
|
||||
try {
|
||||
if(allow_external) {
|
||||
pingSocket = new Socket(Config.QMPServer, Config.QMPPort);
|
||||
pingSocket.setSoTimeout(5000);
|
||||
out = new PrintWriter(pingSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));
|
||||
} else {
|
||||
localSocket = new LocalSocket();
|
||||
String localQMPSocketPath = Config.getLocalQMPSocketPath();
|
||||
LocalSocketAddress localSocketAddr = new LocalSocketAddress(localQMPSocketPath, LocalSocketAddress.Namespace.FILESYSTEM);
|
||||
localSocket.connect(localSocketAddr);
|
||||
localSocket.setSoTimeout(5000);
|
||||
out = new PrintWriter(localSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
|
||||
}
|
||||
|
||||
|
||||
sendRequest(out, QmpClient.requestCommandMode);
|
||||
while(true){
|
||||
response = getResponse(in);
|
||||
if(response == null || response.equals("") || trial <10)
|
||||
break;
|
||||
|
||||
Thread.sleep(1000);
|
||||
trial++;
|
||||
}
|
||||
|
||||
sendRequest(out, command);
|
||||
trial=0;
|
||||
while((response = getResponse(in)).equals("") && trial < 10){
|
||||
Thread.sleep(1000);
|
||||
trial++;
|
||||
}
|
||||
} catch (java.net.ConnectException e) {
|
||||
Log.w(TAG, "Could not connect to QMP: " + e);
|
||||
if(Config.debugQmp)
|
||||
e.printStackTrace();
|
||||
} catch(Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
Log.e(TAG, "Error while connecting to QMP: " + e);
|
||||
if(Config.debugQmp)
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (out != null)
|
||||
out.close();
|
||||
try {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (pingSocket != null)
|
||||
pingSocket.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static void sendRequest(PrintWriter out, String request) {
|
||||
|
||||
if(Config.debugQmp)
|
||||
Log.i(TAG, "QMP request" + request);
|
||||
out.println(request);
|
||||
}
|
||||
|
||||
private static String getResponse(BufferedReader in) throws Exception {
|
||||
|
||||
String line;
|
||||
StringBuilder stringBuilder = new StringBuilder("");
|
||||
|
||||
try {
|
||||
do {
|
||||
line = in.readLine();
|
||||
if (line != null) {
|
||||
if(Config.debugQmp)
|
||||
Log.i(TAG, "QMP response: " + line);
|
||||
JSONObject object = new JSONObject(line);
|
||||
String returnStr = null;
|
||||
String errStr = null;
|
||||
|
||||
try {
|
||||
if(line.contains("return"))
|
||||
returnStr = object.getString("return");
|
||||
} catch (Exception ex) {
|
||||
if(Config.debugQmp)
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
if (returnStr != null) {
|
||||
stringBuilder.append(line);
|
||||
stringBuilder.append("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
if(line.contains("error"))
|
||||
errStr = object.getString("error");
|
||||
} catch (Exception ex) {
|
||||
if(Config.debugQmp)
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
stringBuilder.append(line);
|
||||
stringBuilder.append("\n");
|
||||
|
||||
if (errStr != null) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
} else
|
||||
break;
|
||||
} while (true);
|
||||
} catch (Exception ex) {
|
||||
Log.e(TAG, "Could not get Response: " + ex.getMessage());
|
||||
if(Config.debugQmp)
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
private static String getQueryMigrateResponse(BufferedReader in) throws Exception {
|
||||
|
||||
String line;
|
||||
StringBuilder stringBuilder = new StringBuilder("");
|
||||
|
||||
try {
|
||||
do {
|
||||
line = in.readLine();
|
||||
if (line != null) {
|
||||
if(Config.debugQmp)
|
||||
Log.i(TAG, "QMP query-migrate response: " + line);
|
||||
JSONObject object = new JSONObject(line);
|
||||
String returnStr = null;
|
||||
String errStr = null;
|
||||
|
||||
try {
|
||||
returnStr = object.getString("return");
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
|
||||
if (returnStr != null) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
errStr = object.getString("error");
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
|
||||
stringBuilder.append(line);
|
||||
stringBuilder.append("\n");
|
||||
|
||||
if (errStr != null) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
} else
|
||||
break;
|
||||
} while (true);
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static String migrate(boolean block, boolean inc, String uri) {
|
||||
|
||||
// XXX: Detach should not be used via QMP according to docs
|
||||
// return "{\"execute\":\"migrate\",\"arguments\":{\"detach\":" + detach
|
||||
// + ",\"blk\":" + block + ",\"inc\":" + inc
|
||||
// + ",\"uri\":\"" + uri + "\"},\"id\":\"vectras\"}";
|
||||
|
||||
// its better not to use block (full disk copy) cause its slow (though
|
||||
// safer)
|
||||
// see qmp-commands.hx for more info
|
||||
return "{\"execute\":\"migrate\",\"arguments\":{\"blk\":" + block + ",\"inc\":" + inc + ",\"uri\":\"" + uri
|
||||
+ "\"},\"id\":\"vectras\"}";
|
||||
|
||||
}
|
||||
|
||||
public static String changevncpasswd(String passwd) {
|
||||
|
||||
return "{\"execute\": \"change\", \"arguments\": { \"device\": \"vnc\", \"target\": \"password\", \"arg\": \"" + passwd +"\" } }";
|
||||
|
||||
}
|
||||
|
||||
public static String ejectdev(String dev) {
|
||||
|
||||
return "{ \"execute\": \"eject\", \"arguments\": { \"device\": \""+ dev +"\" } }";
|
||||
|
||||
}
|
||||
|
||||
public static String changedev(String dev, String value) {
|
||||
|
||||
return "{ \"execute\": \"change\", \"arguments\": { \"device\": \""+dev+"\", \"target\": \"" + value + "\" } }";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String query_migrate() {
|
||||
return "{ \"execute\": \"query-migrate\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String save_snapshot(String snapshot_name) {
|
||||
return "{\"execute\": \"snapshot-create\", \"arguments\": {\"name\": \""+ snapshot_name+"\"} }";
|
||||
|
||||
}
|
||||
|
||||
public static String query_snapshot() {
|
||||
return "{ \"execute\": \"query-snapshot-status\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String stop() {
|
||||
return "{ \"execute\": \"stop\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String cont() {
|
||||
return "{ \"execute\": \"cont\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String powerDown() {
|
||||
return "{ \"execute\": \"system_powerdown\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String reset() {
|
||||
return "{ \"execute\": \"system_reset\" }";
|
||||
|
||||
}
|
||||
|
||||
public static String getState() {
|
||||
return "{ \"execute\": \"query-status\" }";
|
||||
|
||||
}
|
||||
}
|
||||
38
app/src/main/java/com/vectras/qemu/utils/RamInfo.java
Normal file
38
app/src/main/java/com/vectras/qemu/utils/RamInfo.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package com.vectras.qemu.utils;
|
||||
|
||||
import static android.content.Context.ACTIVITY_SERVICE;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.vectras.qemu.MainSettingsManager;
|
||||
|
||||
public class RamInfo {
|
||||
public static Activity activity;
|
||||
|
||||
public static int safeLongToInt(long l) {
|
||||
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
|
||||
}
|
||||
return (int) l;
|
||||
}
|
||||
|
||||
public static int vectrasMemory() {
|
||||
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
|
||||
ActivityManager activityManager = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE);
|
||||
activityManager.getMemoryInfo(mi);
|
||||
long freeMem = mi.availMem / 1048576L;
|
||||
long totalMem = mi.totalMem / 1048576L;
|
||||
long usedMem = totalMem - freeMem;
|
||||
int freeRamInt = safeLongToInt(freeMem);
|
||||
int totalRamInt = safeLongToInt(totalMem);
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
||||
if (prefs.getBoolean("customMemory", false)) {
|
||||
return Integer.parseInt(prefs.getString("memory", String.valueOf(256)));
|
||||
} else {
|
||||
return freeRamInt - 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue