Added navigation view

This commit is contained in:
PeratX 2017-04-19 21:13:54 +08:00
parent cd425eb8a5
commit 31dd65440f
14 changed files with 212 additions and 39 deletions

View File

@ -41,7 +41,8 @@
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask">
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@ -10,6 +10,7 @@ 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;
@ -28,6 +29,7 @@ import java.util.Locale;
* the Free Software Foundation, version 3.
*/
public class AboutActivity extends AppCompatActivity {
private WebView mWebView = null;
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled", "addJavascriptInterface"})
@Override
@ -35,13 +37,14 @@ public class AboutActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
WebView view = (WebView) findViewById(R.id.webView_about);
mWebView = new WebView(this.getApplicationContext());
((ViewGroup) findViewById(R.id.activity_about)).addView(mWebView);
view.getSettings().setJavaScriptEnabled(true);
view.setBackgroundColor(0);
view.addJavascriptInterface(this, "JavascriptInterface");
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setBackgroundColor(0);
mWebView.addJavascriptInterface(this, "JavascriptInterface");
view.setOnLongClickListener(new View.OnLongClickListener() {
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
@ -49,17 +52,17 @@ public class AboutActivity extends AppCompatActivity {
});
if (Locale.getDefault().getLanguage().equals("zh")) {
view.loadUrl("file:///android_asset/about_html/index_zh.html");
mWebView.loadUrl("file:///android_asset/about_html/index_zh.html");
} else {
view.loadUrl("file:///android_asset/about_html/index.html");
mWebView.loadUrl("file:///android_asset/about_html/index.html");
}
view.setWebViewClient(new WebViewClient() {
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
try {
view.loadUrl("javascript:changeVersionInfo('" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName + "', '" + BuildConfig.BUILD_TIME + "', '" + BuildConfig.GIT_COMMIT + "')");
mWebView.loadUrl("javascript:changeVersionInfo('" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName + "', '" + BuildConfig.BUILD_TIME + "', '" + BuildConfig.GIT_COMMIT + "')");
} catch (Exception e) {
Log.e("DAboutActivity", e.toString());
}
@ -67,6 +70,23 @@ public class AboutActivity extends AppCompatActivity {
});
}
@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;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

View File

@ -6,12 +6,19 @@ import android.content.Intent;
import android.net.VpnService;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.itxtech.daedalus.BuildConfig;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.service.DaedalusVpnService;
@ -29,7 +36,7 @@ import java.util.List;
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*/
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public static final String LAUNCH_ACTION = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_ACTION";
public static final int LAUNCH_ACTION_NONE = 0;
public static final int LAUNCH_ACTION_ACTIVATE = 1;
@ -48,18 +55,26 @@ public class MainActivity extends AppCompatActivity {
instance = this;
setContentView(R.layout.activity_main);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ServerTestActivity.class);
startActivity(intent);
startActivity(new Intent(v.getContext(), ServerTestActivity.class));
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
final Button but = (Button) findViewById(R.id.button_activate);
if (isServiceActivated()) {
but.setText(R.string.button_text_deactivate);
@ -77,6 +92,9 @@ public class MainActivity extends AppCompatActivity {
}
});
((TextView) navigationView.getHeaderView(0).findViewById(R.id.textView_nav_version)).setText(getString(R.string.nav_version) + " " + BuildConfig.VERSION_NAME);
((TextView) navigationView.getHeaderView(0).findViewById(R.id.textView_nav_git_commit)).setText(getString(R.string.nav_git_commit) + " " + BuildConfig.GIT_COMMIT);
updateUserInterface(getIntent());
}
@ -98,13 +116,13 @@ public class MainActivity extends AppCompatActivity {
Log.d("MainActivity", "Updating user interface");
int launchAction = intent.getIntExtra(LAUNCH_ACTION, LAUNCH_ACTION_NONE);
if (launchAction == LAUNCH_ACTION_ACTIVATE) {
Daedalus.updateShortcut(this);
Daedalus.updateShortcut(this.getApplicationContext());
activateService();
} else if (launchAction == LAUNCH_ACTION_DEACTIVATE) {
deactivateService();
} else {
updateUserInterface();
Daedalus.updateShortcut(this);
Daedalus.updateShortcut(this.getApplicationContext());
}
}
@ -215,4 +233,24 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.action_about) {
startActivity(new Intent(this, AboutActivity.class));
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

View File

@ -123,7 +123,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
this.running = true;
this.mThread.start();
}
Daedalus.updateShortcut(this);
Daedalus.updateShortcut(this.getApplicationContext());
return START_STICKY;
case ACTION_DEACTIVATE:
stopThread();
@ -171,7 +171,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
if (shouldRefresh && MainActivity.getInstance() != null && MainActivity.getInstance().isAppOnForeground()) {
startActivity(new Intent(this, MainActivity.class).putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_NONE));
} else if (shouldRefresh) {
Daedalus.updateShortcut(this);
Daedalus.updateShortcut(this.getApplicationContext());
}
}

View File

@ -0,0 +1,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#81C784"
android:centerColor="#4CAF50"
android:endColor="#2E7D32"
android:type="linear"
android:angle="135"/>
</shape>

View File

@ -8,19 +8,11 @@
<RelativeLayout
android:id="@+id/activity_about"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="org.itxtech.daedalus.activity.AboutActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webView_about"
android:overScrollMode="never"
android:scrollbars="none"
android:layout_alignParentStart="true" android:layout_alignParentTop="true"/>
</RelativeLayout>
</ScrollView>

View File

@ -1,20 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="org.itxtech.daedalus.activity.MainActivity">
android:id="@+id/main_drawer_layout"
tools:openDrawer="start">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@mipmap/ic_verified_user_grey600_36dp"/>
<include layout="@layout/content_main"/>
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="org.itxtech.daedalus.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@mipmap/ic_verified_user_grey600_36dp"/>
<include layout="@layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/imageView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_nav_version"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView_nav_git_commit"/>
</LinearLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
android:id="@+id/nav_group_main">
<item android:id="@+id/nav_home"
android:title="@string/action_home"
android:checked="true"
android:icon="@mipmap/ic_home_grey600_36dp"/>
</group>
<group android:id="@+id/nav_group_more">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:icon="@mipmap/ic_settings_grey600_36dp"/>
<item android:id="@+id/action_about"
android:title="@string/action_about"
android:icon="@mipmap/ic_info_grey600_36dp"/>
</group>
</menu>

View File

@ -38,4 +38,9 @@
<string name="settings_issue_tracker">提交反馈</string>
<string name="settings_issue_summary">前往 GitHub 提交问题反馈。</string>
<string name="settings_dns_test_servers">DNS 测试服务器</string>
<string name="navigation_drawer_close">关闭导航栏</string>
<string name="navigation_drawer_open">开启导航栏</string>
<string name="action_home">主页</string>
<string name="nav_version">版本:</string>
<string name="nav_git_commit">Git commit:</string>
</resources>

View File

@ -3,4 +3,7 @@
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">180dp</dimen>
</resources>

View File

@ -38,4 +38,9 @@
<string name="settings_issue_tracker">Issue tracker</string>
<string name="settings_issue_summary">Submit an issue in GitHub Issues page.</string>
<string name="settings_dns_test_servers">DNS test servers</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_home">Home</string>
<string name="nav_version">Version:</string>
<string name="nav_git_commit">Git commit:</string>
</resources>

View File

@ -2,7 +2,7 @@
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_settings_applications_grey600_36dp"
android:icon="@mipmap/ic_settings_grey600_36dp"
android:shortcutDisabledMessage="@string/action_settings"
android:shortcutId="settings"
android:shortcutLongLabel="@string/action_settings"