Skip to content

Commit

Permalink
Start adding a navigation drawer. Very basic and...
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fat-tire committed Nov 9, 2015
1 parent 515b337 commit 6e47858
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 27 deletions.
182 changes: 158 additions & 24 deletions cSploit/src/main/java/org/csploit/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,168 @@
*/
package org.csploit.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.csploit.android.gui.dialogs.AboutDialog;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

MainFragment f;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
if (themePrefs.getBoolean("isDark", false))
setTheme(R.style.DarkTheme);
else
setTheme(R.style.AppTheme);
setContentView(R.layout.main);
if (findViewById(R.id.mainframe) != null) {
if (savedInstanceState != null) {
return;
}
f = new MainFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.mainframe, f).commit();
}
}

public void onBackPressed() {
f.onBackPressed();
}
MainFragment f;
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout dl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
if (themePrefs.getBoolean("isDark", false))
setTheme(R.style.DarkTheme);
else
setTheme(R.style.AppTheme);
setContentView(R.layout.main);
if (findViewById(R.id.mainframe) != null) {
if (savedInstanceState != null) {
return;
}
dl = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView rv = (ListView) findViewById(R.id.drawer_listview);
String[] items = getResources().getStringArray(R.array.sidebar_item_array);
TypedArray option_icons = getResources().obtainTypedArray(R.array.sidebar_icon_array);
ArrayList<DrawerItem> itemsList = new ArrayList<>();
// load up the drawer with options from the array
for (int x = 0; x < items.length; x++) {
itemsList.add(new DrawerItem(option_icons.getResourceId(x, -1), items[x]));
}
option_icons.recycle();
rv.setAdapter(new SideBarArrayAdapter(this,
R.layout.main_drawer_item, itemsList));
rv.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this,
dl, R.string.drawer_was_opened, R.string.drawer_was_closed);
dl.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

f = new MainFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.mainframe, f).commit();
}
}

public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}

public void onBackPressed() {
f.onBackPressed();
}

public void launchSettings() {
startActivity(new Intent(this, SettingsActivity.class));
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
}

public void launchAbout() {
new AboutDialog(this).show();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

public class DrawerItem {
public int icon;
public String name;

public DrawerItem(int icon, String name) {
this.icon = icon;
this.name = name;
}
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
dl.closeDrawers();
switch (position) {
case 0: //about
launchAbout();
break;
case 1:
launchSettings();
break;
case 2:
String uri = getString(R.string.github_new_issue_url);
Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(browser);
break;
}
}
}

public class SideBarArrayAdapter extends ArrayAdapter<DrawerItem> {

private final Context context;
private final int layoutResourceId;
private ArrayList<DrawerItem> data = null;

public SideBarArrayAdapter(Context context, int layoutResourceId, ArrayList<DrawerItem> data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View v = inflater.inflate(layoutResourceId, parent, false);

ImageView imageView = (ImageView) v.findViewById(R.id.drawer_item_icon);
TextView textView = (TextView) v.findViewById(R.id.drawer_item_title);

DrawerItem item = data.get(position);

imageView.setImageResource(item.icon);
textView.setText(item.name);

return v;
}
}


}
9 changes: 9 additions & 0 deletions cSploit/src/main/res/drawable/ic_bug_report_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF3F9FE0"
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"/>
</vector>
9 changes: 9 additions & 0 deletions cSploit/src/main/res/drawable/ic_info_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF3F9FE0"
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"/>
</vector>
9 changes: 9 additions & 0 deletions cSploit/src/main/res/drawable/ic_settings_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF3F9FE0"
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"/>
</vector>
42 changes: 39 additions & 3 deletions cSploit/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainframe"/>
android:layout_gravity="start"
android:orientation="vertical">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainframe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/background_window"
android:orientation="vertical">

<TextView
android:id="@+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:padding="16dp"
android:text="@string/csploit_catchphrase"
android:textSize="24sp" />

<ListView
android:id="@+id/drawer_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:choiceMode="singleChoice" />
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>
28 changes: 28 additions & 0 deletions cSploit/src/main/res/layout/main_drawer_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">

<ImageView
tools:ignore="contentDescription"
android:id="@+id/drawer_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:maxWidth="40dp"
android:maxHeight="40dp"
android:layout_marginRight="16dp"
android:layout_gravity="center_vertical" />

<TextView
android:id="@+id/drawer_item_title"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:textColor="@color/app_color"
android:textSize="15sp"
android:layout_marginRight="16dp"
android:foregroundGravity="center_vertical"
android:gravity="center_vertical" />
</LinearLayout>
15 changes: 15 additions & 0 deletions cSploit/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="sidebar_item_array">
<item>@string/menu_about</item>
<item>@string/menu_settings</item>
<item>@string/menu_submit_issue</item>
</string-array>
<string-array name="sidebar_icon_array">
<item>@drawable/ic_info_24dp</item>
<item>@drawable/ic_settings_24dp</item>
<item>@drawable/ic_bug_report_24dp</item>
</string-array>

</resources>
3 changes: 3 additions & 0 deletions cSploit/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,7 @@
<string name="connection_lost_prompt">delete current session and start another?</string>
<string name="any_interface">any interface</string>
<string name="error_initializing_interface">Error initializing %s</string>
<string name="drawer_was_opened">The Drawer was opened.</string>
<string name="drawer_was_closed">The Drawer was closed.</string>

</resources>

0 comments on commit 6e47858

Please sign in to comment.