Skip to content

Commit 6e47858

Browse files
committed
Start adding a navigation drawer. Very basic and...
doesn't yet perisist across the whole app. This is because we should: (1) decide what goes in the navigation drawer vs. menus (2) stop loading activities and start loading in fragments (3) think about how to use fragments in a cool way.
1 parent 515b337 commit 6e47858

File tree

8 files changed

+270
-27
lines changed

8 files changed

+270
-27
lines changed

cSploit/src/main/java/org/csploit/android/MainActivity.java

Lines changed: 158 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,168 @@
1919
*/
2020
package org.csploit.android;
2121

22+
import android.app.Activity;
23+
import android.content.Context;
24+
import android.content.Intent;
2225
import android.content.SharedPreferences;
26+
import android.content.res.Configuration;
27+
import android.content.res.TypedArray;
28+
import android.net.Uri;
2329
import android.os.Bundle;
30+
import android.support.v4.widget.DrawerLayout;
31+
import android.support.v7.app.ActionBarDrawerToggle;
2432
import android.support.v7.app.AppCompatActivity;
33+
import android.view.LayoutInflater;
34+
import android.view.MenuItem;
35+
import android.view.View;
36+
import android.view.ViewGroup;
37+
import android.widget.AdapterView;
38+
import android.widget.ArrayAdapter;
39+
import android.widget.ImageView;
40+
import android.widget.ListView;
41+
import android.widget.TextView;
42+
43+
import org.csploit.android.gui.dialogs.AboutDialog;
44+
45+
import java.util.ArrayList;
2546

2647
public class MainActivity extends AppCompatActivity {
2748

28-
MainFragment f;
29-
30-
@Override
31-
protected void onCreate(Bundle savedInstanceState) {
32-
super.onCreate(savedInstanceState);
33-
SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
34-
if (themePrefs.getBoolean("isDark", false))
35-
setTheme(R.style.DarkTheme);
36-
else
37-
setTheme(R.style.AppTheme);
38-
setContentView(R.layout.main);
39-
if (findViewById(R.id.mainframe) != null) {
40-
if (savedInstanceState != null) {
41-
return;
42-
}
43-
f = new MainFragment();
44-
getSupportFragmentManager().beginTransaction()
45-
.add(R.id.mainframe, f).commit();
46-
}
47-
}
48-
49-
public void onBackPressed() {
50-
f.onBackPressed();
51-
}
49+
MainFragment f;
50+
ActionBarDrawerToggle mDrawerToggle;
51+
DrawerLayout dl;
52+
53+
@Override
54+
protected void onCreate(Bundle savedInstanceState) {
55+
super.onCreate(savedInstanceState);
56+
SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
57+
if (themePrefs.getBoolean("isDark", false))
58+
setTheme(R.style.DarkTheme);
59+
else
60+
setTheme(R.style.AppTheme);
61+
setContentView(R.layout.main);
62+
if (findViewById(R.id.mainframe) != null) {
63+
if (savedInstanceState != null) {
64+
return;
65+
}
66+
dl = (DrawerLayout) findViewById(R.id.drawer_layout);
67+
ListView rv = (ListView) findViewById(R.id.drawer_listview);
68+
String[] items = getResources().getStringArray(R.array.sidebar_item_array);
69+
TypedArray option_icons = getResources().obtainTypedArray(R.array.sidebar_icon_array);
70+
ArrayList<DrawerItem> itemsList = new ArrayList<>();
71+
// load up the drawer with options from the array
72+
for (int x = 0; x < items.length; x++) {
73+
itemsList.add(new DrawerItem(option_icons.getResourceId(x, -1), items[x]));
74+
}
75+
option_icons.recycle();
76+
rv.setAdapter(new SideBarArrayAdapter(this,
77+
R.layout.main_drawer_item, itemsList));
78+
rv.setOnItemClickListener(new DrawerItemClickListener());
79+
mDrawerToggle = new ActionBarDrawerToggle(this,
80+
dl, R.string.drawer_was_opened, R.string.drawer_was_closed);
81+
dl.setDrawerListener(mDrawerToggle);
82+
mDrawerToggle.syncState();
83+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
84+
getSupportActionBar().setHomeButtonEnabled(true);
85+
86+
f = new MainFragment();
87+
getSupportFragmentManager().beginTransaction()
88+
.add(R.id.mainframe, f).commit();
89+
}
90+
}
91+
92+
public boolean onOptionsItemSelected(MenuItem item) {
93+
if (mDrawerToggle.onOptionsItemSelected(item)) {
94+
return true;
95+
}
96+
return true;
97+
}
98+
99+
public void onBackPressed() {
100+
f.onBackPressed();
101+
}
102+
103+
public void launchSettings() {
104+
startActivity(new Intent(this, SettingsActivity.class));
105+
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
106+
}
107+
108+
public void launchAbout() {
109+
new AboutDialog(this).show();
110+
}
111+
112+
@Override
113+
protected void onPostCreate(Bundle savedInstanceState) {
114+
super.onPostCreate(savedInstanceState);
115+
// Sync the toggle state after onRestoreInstanceState has occurred.
116+
mDrawerToggle.syncState();
117+
}
118+
119+
@Override
120+
public void onConfigurationChanged(Configuration newConfig) {
121+
super.onConfigurationChanged(newConfig);
122+
mDrawerToggle.onConfigurationChanged(newConfig);
123+
}
124+
125+
public class DrawerItem {
126+
public int icon;
127+
public String name;
128+
129+
public DrawerItem(int icon, String name) {
130+
this.icon = icon;
131+
this.name = name;
132+
}
133+
}
134+
135+
private class DrawerItemClickListener implements ListView.OnItemClickListener {
136+
@Override
137+
public void onItemClick(AdapterView parent, View view, int position, long id) {
138+
dl.closeDrawers();
139+
switch (position) {
140+
case 0: //about
141+
launchAbout();
142+
break;
143+
case 1:
144+
launchSettings();
145+
break;
146+
case 2:
147+
String uri = getString(R.string.github_new_issue_url);
148+
Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
149+
startActivity(browser);
150+
break;
151+
}
152+
}
153+
}
154+
155+
public class SideBarArrayAdapter extends ArrayAdapter<DrawerItem> {
156+
157+
private final Context context;
158+
private final int layoutResourceId;
159+
private ArrayList<DrawerItem> data = null;
160+
161+
public SideBarArrayAdapter(Context context, int layoutResourceId, ArrayList<DrawerItem> data) {
162+
super(context, layoutResourceId, data);
163+
this.context = context;
164+
this.layoutResourceId = layoutResourceId;
165+
this.data = data;
166+
}
167+
168+
@Override
169+
public View getView(int position, View convertView, ViewGroup parent) {
170+
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
171+
View v = inflater.inflate(layoutResourceId, parent, false);
172+
173+
ImageView imageView = (ImageView) v.findViewById(R.id.drawer_item_icon);
174+
TextView textView = (TextView) v.findViewById(R.id.drawer_item_title);
175+
176+
DrawerItem item = data.get(position);
177+
178+
imageView.setImageResource(item.icon);
179+
textView.setText(item.name);
180+
181+
return v;
182+
}
183+
}
184+
185+
52186
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF3F9FE0"
8+
android:pathData="M20,8h-2.81c-0.45,-0.78 -1.07,-1.45 -1.82,-1.96L17,4.41 15.59,3l-2.17,2.17C12.96,5.06 12.49,5 12,5c-0.49,0 -0.96,0.06 -1.41,0.17L8.41,3 7,4.41l1.62,1.63C7.88,6.55 7.26,7.22 6.81,8H4v2h2.09c-0.05,0.33 -0.09,0.66 -0.09,1v1H4v2h2v1c0,0.34 0.04,0.67 0.09,1H4v2h2.81c1.04,1.79 2.97,3 5.19,3s4.15,-1.21 5.19,-3H20v-2h-2.09c0.05,-0.33 0.09,-0.66 0.09,-1v-1h2v-2h-2v-1c0,-0.34 -0.04,-0.67 -0.09,-1H20V8zm-6,8h-4v-2h4v2zm0,-4h-4v-2h4v2z"/>
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF3F9FE0"
8+
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zm1,15h-2v-6h2v6zm0,-8h-2V7h2v2z"/>
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF3F9FE0"
8+
android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>
9+
</vector>
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical" android:layout_width="match_parent"
2+
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
4+
android:id="@+id/drawer_layout"
5+
android:layout_width="match_parent"
46
android:layout_height="match_parent"
5-
android:id="@+id/mainframe"/>
7+
android:layout_gravity="start"
8+
android:orientation="vertical">
9+
10+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
11+
android:id="@+id/mainframe"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:orientation="vertical" />
15+
16+
<RelativeLayout
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
android:layout_gravity="start"
20+
android:background="@color/background_window"
21+
android:orientation="vertical">
22+
23+
<TextView
24+
android:id="@+id/app_name"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_alignParentTop="true"
28+
android:layout_gravity="top"
29+
android:padding="16dp"
30+
android:text="@string/csploit_catchphrase"
31+
android:textSize="24sp" />
32+
33+
<ListView
34+
android:id="@+id/drawer_listview"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_alignParentBottom="true"
38+
android:choiceMode="singleChoice" />
39+
</RelativeLayout>
40+
41+
</android.support.v4.widget.DrawerLayout>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="48dp"
6+
android:orientation="horizontal">
7+
8+
<ImageView
9+
tools:ignore="contentDescription"
10+
android:id="@+id/drawer_item_icon"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_marginLeft="16dp"
14+
android:maxWidth="40dp"
15+
android:maxHeight="40dp"
16+
android:layout_marginRight="16dp"
17+
android:layout_gravity="center_vertical" />
18+
19+
<TextView
20+
android:id="@+id/drawer_item_title"
21+
android:layout_width="fill_parent"
22+
android:layout_height="48dp"
23+
android:textColor="@color/app_color"
24+
android:textSize="15sp"
25+
android:layout_marginRight="16dp"
26+
android:foregroundGravity="center_vertical"
27+
android:gravity="center_vertical" />
28+
</LinearLayout>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string-array
4+
name="sidebar_item_array">
5+
<item>@string/menu_about</item>
6+
<item>@string/menu_settings</item>
7+
<item>@string/menu_submit_issue</item>
8+
</string-array>
9+
<string-array name="sidebar_icon_array">
10+
<item>@drawable/ic_info_24dp</item>
11+
<item>@drawable/ic_settings_24dp</item>
12+
<item>@drawable/ic_bug_report_24dp</item>
13+
</string-array>
14+
15+
</resources>

cSploit/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,7 @@
537537
<string name="connection_lost_prompt">delete current session and start another?</string>
538538
<string name="any_interface">any interface</string>
539539
<string name="error_initializing_interface">Error initializing %s</string>
540+
<string name="drawer_was_opened">The Drawer was opened.</string>
541+
<string name="drawer_was_closed">The Drawer was closed.</string>
542+
540543
</resources>

0 commit comments

Comments
 (0)