Basic export/import
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 74 KiB |
|
@ -28,6 +28,7 @@ import android.support.v7.widget.RecyclerView;
|
|||
import android.support.v7.widget.SearchView;
|
||||
import android.support.v7.widget.SwitchCompat;
|
||||
import android.util.Log;
|
||||
import android.util.Xml;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
|
@ -41,9 +42,24 @@ import android.widget.Toast;
|
|||
import com.android.vending.billing.IInAppBillingService;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
public class ActivityMain extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final String TAG = "NetGuard.Main";
|
||||
|
@ -332,6 +348,14 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
|
|||
menu_system(prefs);
|
||||
return true;
|
||||
|
||||
case R.id.menu_export:
|
||||
menu_export();
|
||||
return true;
|
||||
|
||||
case R.id.menu_import:
|
||||
menu_import();
|
||||
return true;
|
||||
|
||||
case R.id.menu_theme:
|
||||
menu_theme(prefs);
|
||||
return true;
|
||||
|
@ -380,6 +404,29 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
|
|||
SinkholeService.reload(null, this);
|
||||
}
|
||||
|
||||
private void menu_export() {
|
||||
try {
|
||||
File target = new File(getExternalCacheDir(), "netguard.xml");
|
||||
Log.i(TAG, "Writing file=" + target);
|
||||
xmlExport(target);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void menu_import() {
|
||||
try {
|
||||
File target = new File(getExternalCacheDir(), "netguard.xml");
|
||||
Log.i(TAG, "Reading file=" + target);
|
||||
xmlImport(target);
|
||||
recreate();
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void menu_theme(SharedPreferences prefs) {
|
||||
prefs.edit().putBoolean("dark_theme", !prefs.getBoolean("dark_theme", false)).apply();
|
||||
recreate();
|
||||
|
@ -558,6 +605,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
|
|||
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -609,4 +657,136 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
|
|||
return Integer.toString(responseCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void xmlExport(File target) throws IOException {
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream(target);
|
||||
|
||||
XmlSerializer serializer = Xml.newSerializer();
|
||||
serializer.setOutput(out, "UTF-8");
|
||||
serializer.startDocument(null, Boolean.valueOf(true));
|
||||
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
|
||||
serializer.startTag(null, "netguard");
|
||||
|
||||
serializer.startTag(null, "application");
|
||||
xmlExport(PreferenceManager.getDefaultSharedPreferences(this), serializer);
|
||||
serializer.endTag(null, "application");
|
||||
|
||||
serializer.startTag(null, "wifi");
|
||||
xmlExport(getSharedPreferences("wifi", Context.MODE_PRIVATE), serializer);
|
||||
serializer.endTag(null, "wifi");
|
||||
|
||||
serializer.startTag(null, "mobile");
|
||||
xmlExport(getSharedPreferences("other", Context.MODE_PRIVATE), serializer);
|
||||
serializer.endTag(null, "mobile");
|
||||
|
||||
serializer.startTag(null, "unused");
|
||||
xmlExport(getSharedPreferences("unused", Context.MODE_PRIVATE), serializer);
|
||||
serializer.endTag(null, "unused");
|
||||
|
||||
serializer.endTag(null, "netguard");
|
||||
serializer.endDocument();
|
||||
serializer.flush();
|
||||
} finally {
|
||||
if (out != null)
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void xmlExport(SharedPreferences prefs, XmlSerializer serializer) throws IOException {
|
||||
Map<String, ?> settings = prefs.getAll();
|
||||
for (String key : settings.keySet()) {
|
||||
Object value = settings.get(key);
|
||||
if (value instanceof Boolean) {
|
||||
serializer.startTag(null, "setting");
|
||||
serializer.attribute(null, "key", key);
|
||||
serializer.attribute(null, "type", "boolean");
|
||||
serializer.attribute(null, "value", value.toString());
|
||||
serializer.endTag(null, "setting");
|
||||
} else
|
||||
Log.e(TAG, "Unknown key=" + key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void xmlImport(File file) throws IOException, SAXException, ParserConfigurationException {
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
|
||||
XmlImportHandler handler = new XmlImportHandler();
|
||||
reader.setContentHandler(handler);
|
||||
reader.parse(new InputSource(in));
|
||||
|
||||
xmlImport(handler.application, PreferenceManager.getDefaultSharedPreferences(this));
|
||||
xmlImport(handler.wifi, getSharedPreferences("wifi", Context.MODE_PRIVATE));
|
||||
xmlImport(handler.mobile, getSharedPreferences("other", Context.MODE_PRIVATE));
|
||||
xmlImport(handler.unused, getSharedPreferences("unused", Context.MODE_PRIVATE));
|
||||
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void xmlImport(Map<String, Object> settings, SharedPreferences prefs) {
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
|
||||
for (String key : prefs.getAll().keySet())
|
||||
editor.remove(key);
|
||||
|
||||
for (String key : settings.keySet()) {
|
||||
Object value = settings.get(key);
|
||||
if (value instanceof Boolean)
|
||||
editor.putBoolean(key, (Boolean) value);
|
||||
else
|
||||
Log.e(TAG, "Unknown type=" + value.getClass());
|
||||
}
|
||||
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private class XmlImportHandler extends DefaultHandler {
|
||||
public Map<String, Object> application = new HashMap<>();
|
||||
public Map<String, Object> wifi = new HashMap<>();
|
||||
public Map<String, Object> mobile = new HashMap<>();
|
||||
public Map<String, Object> unused = new HashMap<>();
|
||||
private Map<String, Object> current = null;
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) {
|
||||
if (qName.equals("netguard"))
|
||||
; // Ignore
|
||||
|
||||
else if (qName.equals("application"))
|
||||
current = application;
|
||||
|
||||
else if (qName.equals("wifi"))
|
||||
current = wifi;
|
||||
|
||||
else if (qName.equals("mobile"))
|
||||
current = mobile;
|
||||
|
||||
else if (qName.equals("unused"))
|
||||
current = unused;
|
||||
|
||||
else if (qName.equals("setting")) {
|
||||
String key = attributes.getValue("key");
|
||||
String type = attributes.getValue("type");
|
||||
String value = attributes.getValue("value");
|
||||
|
||||
if (current == null)
|
||||
Log.e(TAG, "No current key=" + key);
|
||||
else {
|
||||
if ("boolean".equals(type))
|
||||
current.put(key, Boolean.parseBoolean(value));
|
||||
else
|
||||
Log.e(TAG, "Unknown type key=" + key);
|
||||
}
|
||||
|
||||
} else
|
||||
Log.e(TAG, "Unknown element qname=" + qName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,6 +23,12 @@
|
|||
android:id="@+id/menu_system"
|
||||
android:checkable="true"
|
||||
android:title="@string/menu_system" />
|
||||
<item
|
||||
android:id="@+id/menu_export"
|
||||
android:title="@string/menu_export" />
|
||||
<item
|
||||
android:id="@+id/menu_import"
|
||||
android:title="@string/menu_import" />
|
||||
<item
|
||||
android:id="@+id/menu_theme"
|
||||
android:checkable="true"
|
||||
|
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 765 B After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 14 KiB |
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">حظر الواي فاي بشكل افتراضي</string>
|
||||
<string name="menu_whitelist_other">حظر الشبكة بشكل افتراضي</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">استخدام الثيم الداكن</string>
|
||||
<string name="menu_vpn_settings">فتح إعدادات الـVPN</string>
|
||||
<string name="menu_support">الدعم</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Blocage Wi-Fi par défaut</string>
|
||||
<string name="menu_whitelist_other">Blocage données mobiles par défaut</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">Utiliser le thème sombre</string>
|
||||
<string name="menu_vpn_settings">Ouvrir les paramètres VPN</string>
|
||||
<string name="menu_support">Support</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Blocca Wi-Fi di default</string>
|
||||
<string name="menu_whitelist_other">Block rete di dati di default</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">Usa il tema scuro</string>
|
||||
<string name="menu_vpn_settings">Apri impostazioni VPN</string>
|
||||
<string name="menu_support">Supporto</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Wi-Fi 차단을 기본 설정으로</string>
|
||||
<string name="menu_whitelist_other">모바일 데이터 차단을 기본 설정으로</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">어두운 테마 사용</string>
|
||||
<string name="menu_vpn_settings">Android VPN 설정 열기</string>
|
||||
<string name="menu_support">지원</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Blokkeer Wi-Fi standaard</string>
|
||||
<string name="menu_whitelist_other">Blokkeer mobiel standaard</string>
|
||||
<string name="menu_system">Beheer systeemapplicaties</string>
|
||||
<string name="menu_export">Exporteer instellingen</string>
|
||||
<string name="menu_import">Importeer instellingen</string>
|
||||
<string name="menu_dark">Gebruik donker thema</string>
|
||||
<string name="menu_vpn_settings">Open Android VPN-instellingen</string>
|
||||
<string name="menu_support">Ondersteuning</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Blocheaza implicit Wi-Fi</string>
|
||||
<string name="menu_whitelist_other">Blocheaza implicit date mobile</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">Foloseste tema intunecata</string>
|
||||
<string name="menu_vpn_settings">Deschide setari VPN Android</string>
|
||||
<string name="menu_support">Asistenta</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">Predvolene blokovať Wi-Fi</string>
|
||||
<string name="menu_whitelist_other">Predvolene blokovať mobilné dáta</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">Použiť tmavú tému</string>
|
||||
<string name="menu_vpn_settings">Otvoriť nastavenia VPN</string>
|
||||
<string name="menu_support">Podpora</string>
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
<string name="menu_whitelist_wifi">默认阻止Wi-Fi网络</string>
|
||||
<string name="menu_whitelist_other">默认阻止移动网络</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">使用暗色主题</string>
|
||||
<string name="menu_vpn_settings">打开安卓VPN设置</string>
|
||||
<string name="menu_support">软件支持</string>
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
<string name="menu_whitelist_wifi">Block Wi-Fi by default</string>
|
||||
<string name="menu_whitelist_other">Block mobile by default</string>
|
||||
<string name="menu_system">Manage system applications</string>
|
||||
<string name="menu_export">Export settings</string>
|
||||
<string name="menu_import">Import settings</string>
|
||||
<string name="menu_dark">Use dark theme</string>
|
||||
<string name="menu_vpn_settings">Open Android VPN settings</string>
|
||||
<string name="menu_support">Support</string>
|
||||
|
|