Implemented hosts auto downloader
This commit is contained in:
parent
7e18e494a9
commit
e7f3290ed1
@ -59,6 +59,8 @@ public class Daedalus extends Application {
|
||||
"wikipedia.org"
|
||||
};
|
||||
|
||||
public static String hostsPath;
|
||||
|
||||
private static Daedalus instance = null;
|
||||
private static SharedPreferences prefs;
|
||||
private static Thread mHostsResolver;
|
||||
@ -71,6 +73,8 @@ public class Daedalus extends Application {
|
||||
mHostsResolver = new Thread(new HostsResolver());
|
||||
mHostsResolver.start();
|
||||
|
||||
hostsPath = getExternalFilesDir(null).getPath() + "/hosts";
|
||||
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@ -97,7 +101,7 @@ public class Daedalus extends Application {
|
||||
return;
|
||||
}
|
||||
}
|
||||
HostsResolver.startLoad(instance.getExternalFilesDir(null).getPath() + "/hosts");
|
||||
HostsResolver.startLoad(hostsPath);
|
||||
}
|
||||
}
|
||||
public static SharedPreferences getPrefs() {
|
||||
|
@ -2,15 +2,23 @@ package org.itxtech.daedalus.fragment;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.Spinner;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.R;
|
||||
import org.itxtech.daedalus.util.HostsProvider;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
* Daedalus Project
|
||||
*
|
||||
@ -22,14 +30,111 @@ import org.itxtech.daedalus.util.HostsProvider;
|
||||
* the Free Software Foundation, version 3.
|
||||
*/
|
||||
public class HostsFragment extends Fragment {
|
||||
|
||||
private Thread mThread = null;
|
||||
private View view = null;
|
||||
private HostsHandler mHandler = null;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_hosts, container, false);
|
||||
view = inflater.inflate(R.layout.fragment_hosts, container, false);
|
||||
|
||||
Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
|
||||
mHandler = new HostsHandler().setView(view);
|
||||
|
||||
final Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
|
||||
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(Daedalus.getInstance(), android.R.layout.simple_list_item_1, HostsProvider.getHostsProviderNames());
|
||||
spinnerHosts.setAdapter(spinnerArrayAdapter);
|
||||
spinnerHosts.setSelection(0);
|
||||
|
||||
Button buttonDownload = (Button) view.findViewById(R.id.button_download_hosts);
|
||||
buttonDownload.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mThread == null) {
|
||||
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
mThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URLConnection connection = new URL(HostsProvider.getDownloadUrlByName(spinnerHosts.getSelectedItem().toString())).openConnection();
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String result;
|
||||
while ((result = reader.readLine()) != null) {
|
||||
builder.append("\n").append(result);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
mHandler.obtainMessage(HostsHandler.MSG_DOWNLOADED, builder.toString()).sendToTarget();
|
||||
stopThread();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
mThread.start();
|
||||
} else {
|
||||
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
stopThread();
|
||||
mHandler.shutdown();
|
||||
mHandler = null;
|
||||
view = null;
|
||||
}
|
||||
|
||||
private void stopThread() {
|
||||
if (mThread != null) {
|
||||
mThread.interrupt();
|
||||
mThread = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class HostsHandler extends Handler {
|
||||
static final int MSG_DOWNLOADED = 0;
|
||||
|
||||
private View view = null;
|
||||
|
||||
HostsHandler setView(View view) {
|
||||
this.view = view;
|
||||
return this;
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
view = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
|
||||
switch (msg.what) {
|
||||
case MSG_DOWNLOADED:
|
||||
try {
|
||||
String result = (String) msg.obj;
|
||||
File file = new File(Daedalus.hostsPath);
|
||||
FileOutputStream stream = new FileOutputStream(file);
|
||||
stream.write(result.getBytes());
|
||||
stream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,4 +39,13 @@ public class HostsProvider {
|
||||
String[] stringServers = new String[Daedalus.HOSTS_PROVIDERS.size()];
|
||||
return servers.toArray(stringServers);
|
||||
}
|
||||
|
||||
public static String getDownloadUrlByName(String name) {
|
||||
for (HostsProvider hostsProvider : Daedalus.HOSTS_PROVIDERS) {
|
||||
if (hostsProvider.getName().equals(name)) {
|
||||
return hostsProvider.getDownloadURL();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,10 @@
|
||||
<string name="settings_advanced_on">开启</string>
|
||||
<string name="settings_local_hosts_resolve">本地 hosts 解析</string>
|
||||
<string name="notice_need_restart">重新启用 Daedalus 以应用设置。</string>
|
||||
<string name="notice_need_storage_perm">本应用需要访问外部储存以实现本地 hosts 解析。</string>
|
||||
<string name="notice_need_storage_perm">Daedalus 需要访问外部储存以实现本地 hosts 解析。</string>
|
||||
<string name="action_hosts">Hosts</string>
|
||||
<string name="button_text_download_hosts">下载 hosts</string>
|
||||
<string name="notice_start_download">正在下载 hosts,请稍等 ……</string>
|
||||
<string name="notice_downloaded">已下载 hosts 。</string>
|
||||
<string name="notice_now_downloading">Daedalus 当前正在下载hosts,请稍等。</string>
|
||||
</resources>
|
@ -47,9 +47,11 @@
|
||||
<string name="settings_advanced_on">On</string>
|
||||
<string name="settings_local_hosts_resolve">Local hosts resolution</string>
|
||||
<string name="notice_need_restart">Re-activate Daedalus to make the settings take effect.</string>
|
||||
<string name="notice_need_storage_perm">This application requires access to external storage for local host
|
||||
resolution.
|
||||
<string name="notice_need_storage_perm">Daedalus requires access to external storage for local host resolution.
|
||||
</string>
|
||||
<string name="action_hosts">Hosts</string>
|
||||
<string name="button_text_download_hosts">Download hosts</string>
|
||||
<string name="notice_start_download">Downloading hosts, please wait …</string>
|
||||
<string name="notice_downloaded">Hosts has been downloaded.</string>
|
||||
<string name="notice_now_downloading">Daedalus is currently downloading hosts, please wait.</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user