19
19
*/
20
20
package org .csploit .android ;
21
21
22
+ import android .app .Activity ;
23
+ import android .content .Context ;
24
+ import android .content .Intent ;
22
25
import android .content .SharedPreferences ;
26
+ import android .content .res .Configuration ;
27
+ import android .content .res .TypedArray ;
28
+ import android .net .Uri ;
23
29
import android .os .Bundle ;
30
+ import android .support .v4 .widget .DrawerLayout ;
31
+ import android .support .v7 .app .ActionBarDrawerToggle ;
24
32
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 ;
25
46
26
47
public class MainActivity extends AppCompatActivity {
27
48
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
+
52
186
}
0 commit comments