Improved MainFragment UI update

This commit is contained in:
PeratX 2017-04-24 19:52:16 +08:00
parent 32ba5fce33
commit b55651877b
6 changed files with 70 additions and 170 deletions

View File

@ -1,151 +0,0 @@
package org.itxtech.daedalus.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.itxtech.daedalus.BuildConfig;
import org.itxtech.daedalus.R;
import java.util.Locale;
/**
* Daedalus Project
*
* @author iTXTech
* @link https://itxtech.org
* <p>
* 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, version 3.
*/
public class AboutActivity extends AppCompatActivity {
private WebView mWebView = null;
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled", "addJavascriptInterface"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
mWebView = new WebView(this.getApplicationContext());
((ViewGroup) findViewById(R.id.activity_about)).addView(mWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setBackgroundColor(0);
mWebView.addJavascriptInterface(this, "JavascriptInterface");
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
if (Locale.getDefault().getLanguage().equals("zh")) {
mWebView.loadUrl("file:///android_asset/about_html/index_zh.html");
} else {
mWebView.loadUrl("file:///android_asset/about_html/index.html");
}
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {//for better compatibility
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
try {
mWebView.loadUrl("javascript:changeVersionInfo('" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName + "', '" + BuildConfig.BUILD_TIME + "', '" + BuildConfig.GIT_COMMIT + "')");
} catch (Exception e) {
Log.e("DAboutActivity", e.toString());
}
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (mWebView != null) {
Log.d("DAboutActivity", "onDestroy");
mWebView.removeAllViews();
mWebView.setWebViewClient(null);
((ViewGroup) mWebView.getParent()).removeView(mWebView);
mWebView.setTag(null);
mWebView.clearHistory();
mWebView.destroy();
mWebView = null;
}
System.gc();
//System.exit(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_visit_itxtech) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://itxtech.org")));
}
if (id == R.id.action_visit_github) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus")));
}
if (id == R.id.action_license) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/blob/master/LICENSE")));
}
/*if (id == R.id.action_visit_cutedns) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.cutedns.cn")));
}*/
/*if (id == R.id.action_join_qqgroup) {
joinQQGroup("q6Lfo_EhAEO1fP6Xg3fmKsP4pd6U5-RE");
}*/
return super.onOptionsItemSelected(item);
}
private boolean joinQQGroup(String key) {
Intent intent = new Intent();
intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
// 此Flag可根据具体产品需要自定义如设置则在加群界面按返回返回手Q主界面不设置按返回会返回到呼起产品界面 //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
try {
startActivity(intent);
return true;
} catch (Exception e) {
Snackbar.make(findViewById(R.id.activity_about), R.string.notice_join_group_failed, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return false;
}
}
}

View File

@ -38,6 +38,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public static final int LAUNCH_ACTION_NONE = 0;
public static final int LAUNCH_ACTION_ACTIVATE = 1;
public static final int LAUNCH_ACTION_DEACTIVATE = 2;
public static final int LAUNCH_ACTION_AFTRER_DEACTIVATE = 3;
public static final String LAUNCH_FRAGMENT = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_FRAGMENT";
private static final String TAG = "DMainActivity";
@ -56,6 +57,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private AboutFragment mAbout;
private int currentFragment = FRAGMENT_NONE;
private MainFragment.MainFragmentHandler mHandler = null;
public void setMainFragmentHandler(MainFragment.MainFragmentHandler mHandler) {
this.mHandler = mHandler;
}
public static MainActivity getInstance() {
return instance;
}
@ -222,6 +229,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
mMain.activateService();
} else if (launchAction == LAUNCH_ACTION_DEACTIVATE) {
Daedalus.getInstance().deactivateService();
} else if (launchAction == LAUNCH_ACTION_AFTRER_DEACTIVATE) {
Daedalus.updateShortcut(this.getApplicationContext());
mHandler.obtainMessage(MainFragment.MainFragmentHandler.MSG_REFRESH).sendToTarget();
} else {
Daedalus.updateShortcut(this.getApplicationContext());
}
@ -255,7 +265,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
if (id == R.id.nav_github) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/issues")));
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus")));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);

View File

@ -177,7 +177,7 @@ public class DNSTestFragment extends Fragment {
}
}
public static class ServerTestHandler extends Handler {
private static class ServerTestHandler extends Handler {
private Button startTestBtn = null;
private TextView textViewTestInfo = null;

View File

@ -2,15 +2,20 @@ package org.itxtech.daedalus.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.VpnService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServer;
@ -27,6 +32,7 @@ import org.itxtech.daedalus.util.DnsServer;
public class MainFragment extends Fragment {
private View view = null;
private MainFragmentHandler mHandler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -38,11 +44,6 @@ public class MainFragment extends Fragment {
view = inflater.inflate(R.layout.fragment_main, container, false);
final Button but = (Button) view.findViewById(R.id.button_activate);
if (Daedalus.getInstance().isServiceActivated()) {
but.setText(R.string.button_text_deactivate);
} else {
but.setText(R.string.button_text_activate);
}
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -59,6 +60,24 @@ public class MainFragment extends Fragment {
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MainActivity) {
mHandler = (new MainFragmentHandler()).setFragment(this);
((MainActivity) context).setMainFragmentHandler(mHandler);
}
}
@Override
public void onDetach() {
super.onDetach();
mHandler.shutdown();
MainActivity.getInstance().setMainFragmentHandler(null);
}
@Override
public void onDestroy() {
super.onDestroy();
@ -68,6 +87,7 @@ public class MainFragment extends Fragment {
public void activateService() {
Intent intent = VpnService.prepare(Daedalus.getInstance());
view.findViewById(R.id.button_activate).setVisibility(View.INVISIBLE);
if (intent != null) {
startActivityForResult(intent, 0);
} else {
@ -82,8 +102,9 @@ public class MainFragment extends Fragment {
Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE));
((Button) view.findViewById(R.id.button_activate)).setText(R.string.button_text_deactivate);
Button button = (Button) view.findViewById(R.id.button_activate);
button.setText(R.string.button_text_deactivate);
button.setVisibility(View.VISIBLE);
}
}
@ -94,6 +115,7 @@ public class MainFragment extends Fragment {
}
private void updateUserInterface() {
Log.d("DMainFragment", "updateInterface");
Button but = (Button) view.findViewById(R.id.button_activate);
if (Daedalus.getInstance().isServiceActivated()) {
but.setText(R.string.button_text_deactivate);
@ -102,4 +124,30 @@ public class MainFragment extends Fragment {
}
}
public static class MainFragmentHandler extends Handler {
public static final int MSG_REFRESH = 0;
private MainFragment fragment = null;
MainFragmentHandler setFragment(MainFragment fragment) {
this.fragment = fragment;
return this;
}
void shutdown() {
fragment = null;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_REFRESH:
((Button) fragment.view.findViewById(R.id.button_activate)).setText(R.string.button_text_activate);
break;
}
}
}
}

View File

@ -4,10 +4,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import java.lang.reflect.Method;
@ -28,12 +26,7 @@ public class StatusBarBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(STATUS_BAR_BTN_DEACTIVATE_CLICK_ACTION)) {
Intent serviceIntent = new Intent(context, DaedalusVpnService.class);
context.startService(serviceIntent.setAction(DaedalusVpnService.ACTION_DEACTIVATE));
context.stopService(serviceIntent);
if (MainActivity.getInstance() != null) {
((Button) MainActivity.getInstance().findViewById(R.id.button_activate)).setText(R.string.button_text_activate);
}
Daedalus.getInstance().deactivateService();
}
if (intent.getAction().equals(STATUS_BAR_BTN_SETTINGS_CLICK_ACTION)) {
Intent settingsIntent = new Intent(context, MainActivity.class).putExtra(MainActivity.LAUNCH_FRAGMENT, MainActivity.FRAGMENT_SETTINGS);

View File

@ -168,7 +168,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
stopSelf();
if (shouldRefresh && MainActivity.getInstance() != null && Daedalus.getInstance().isAppOnForeground()) {
MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class).putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_NONE));
MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class).putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_AFTRER_DEACTIVATE));
} else if (shouldRefresh) {
Daedalus.updateShortcut(getApplicationContext());
}