1
1
package at .tacticaldevc .panictrigger ;
2
2
3
- import android .Manifest ;
4
3
import android .content .DialogInterface ;
5
4
import android .content .Intent ;
6
- import android .content .pm .PackageManager ;
7
5
import android .location .Location ;
8
6
import android .location .LocationListener ;
9
7
import android .location .LocationManager ;
10
8
import android .net .Uri ;
11
9
import android .os .Bundle ;
10
+ import android .os .Vibrator ;
11
+ import android .support .annotation .NonNull ;
12
12
import android .support .v7 .app .AlertDialog ;
13
13
import android .support .v7 .app .AppCompatActivity ;
14
14
import android .telephony .SmsManager ;
15
15
import android .view .View ;
16
+ import android .widget .ArrayAdapter ;
17
+ import android .widget .Spinner ;
16
18
import android .widget .Toast ;
17
19
18
20
import java .util .HashSet ;
19
21
import java .util .Set ;
20
22
23
+ import at .tacticaldevc .panictrigger .contactList .Contact ;
24
+ import at .tacticaldevc .panictrigger .utils .Utils ;
25
+
21
26
public class TriggerActivity extends AppCompatActivity implements View .OnClickListener , LocationListener
22
27
{
28
+ private Vibrator v ;
29
+ private Contact [] notifyContacts ;
23
30
24
31
@ Override
25
32
protected void onCreate (Bundle savedInstanceState ) {
26
33
super .onCreate (savedInstanceState );
27
34
setContentView (R .layout .activity_trigger );
28
35
29
- if (checkSelfPermission (Manifest .permission .RECEIVE_SMS ) != PackageManager .PERMISSION_GRANTED ||
30
- checkSelfPermission (Manifest .permission .SEND_SMS ) != PackageManager .PERMISSION_GRANTED ||
31
- checkSelfPermission (Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ||
32
- checkSelfPermission (Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED ||
33
- checkSelfPermission (Manifest .permission .CALL_PHONE ) != PackageManager .PERMISSION_GRANTED ||
34
- checkSelfPermission (Manifest .permission .READ_CONTACTS ) != PackageManager .PERMISSION_GRANTED ||
35
- checkSelfPermission (Manifest .permission .CALL_PRIVILEGED ) != PackageManager .PERMISSION_GRANTED ||
36
- checkSelfPermission (Manifest .permission .INTERNET ) != PackageManager .PERMISSION_GRANTED ||
37
- checkSelfPermission (Manifest .permission .ACCESS_NETWORK_STATE ) != PackageManager .PERMISSION_GRANTED ||
38
- checkSelfPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE ) != PackageManager .PERMISSION_GRANTED )
39
- {
40
- requestPermissions (new String []{
41
- Manifest .permission .RECEIVE_SMS ,
42
- Manifest .permission .SEND_SMS ,
43
- Manifest .permission .ACCESS_COARSE_LOCATION ,
44
- Manifest .permission .ACCESS_FINE_LOCATION ,
45
- Manifest .permission .CALL_PHONE ,
46
- Manifest .permission .READ_CONTACTS ,
47
- Manifest .permission .CALL_PRIVILEGED ,
48
- Manifest .permission .INTERNET ,
49
- Manifest .permission .ACCESS_NETWORK_STATE ,
50
- Manifest .permission .WRITE_EXTERNAL_STORAGE ,
51
- }, 1 );
52
- }
36
+ v = (Vibrator ) getSystemService (VIBRATOR_SERVICE );
37
+
38
+ String [] perms ;
39
+ if ((perms = Utils .checkPermissions (this )).length > 0 )
40
+ requestPermissions (perms , 255 );
53
41
54
42
if (!getSharedPreferences ("conf" , MODE_PRIVATE ).getBoolean ("firstStartDone" , false ))
55
43
{
@@ -92,11 +80,7 @@ public void onClick(DialogInterface dialog, int which) {
92
80
startActivity (new Intent (TriggerActivity .this , SettingsActivity .class ));
93
81
}
94
82
})
95
- .setNegativeButton ("Yes." , new DialogInterface .OnClickListener () {
96
- @ Override
97
- public void onClick (DialogInterface dialog , int which ) {
98
- }
99
- })
83
+ .setNegativeButton ("Yes." , null )
100
84
.show ();
101
85
}
102
86
})
@@ -130,7 +114,28 @@ public void onClick(View v) {
130
114
switch (v .getId ())
131
115
{
132
116
case R .id .triggerButton :
133
- getCurrentLocationAndPanic ();
117
+ if (callEmergServices ())
118
+ {
119
+ Intent emergService = new Intent (Intent .ACTION_CALL , Uri .parse ("tel:112" ));
120
+ startActivity (emergService );
121
+ return ;
122
+ }
123
+
124
+ this .v .vibrate (1000 );
125
+ final View content = getLayoutInflater ().inflate (R .layout .content_dialog_trigger_group_select , null );
126
+ final ArrayAdapter <String > ad = new ArrayAdapter <String >(this , R .layout .support_simple_spinner_dropdown_item , Utils .getContactGroups (this ));
127
+ ((Spinner )content .findViewById (R .id .emergency_group )).setAdapter (ad );
128
+
129
+ new AlertDialog .Builder (this )
130
+ .setView (content )
131
+ .setPositiveButton ("Trigger" , new DialogInterface .OnClickListener () {
132
+ @ Override
133
+ public void onClick (DialogInterface dialog , int which ) {
134
+ notifyContacts = Utils .getContactsByGroup (((Spinner )content .findViewById (R .id .emergency_group )).getSelectedItem ().toString (), TriggerActivity .this );
135
+ getCurrentLocationAndPanic ();
136
+ }
137
+ })
138
+ .show ();
134
139
break ;
135
140
case R .id .configure :
136
141
Intent settings = new Intent (this , SettingsActivity .class );
@@ -141,28 +146,20 @@ public void onClick(View v) {
141
146
142
147
private void sendOutPanic (Location loc )
143
148
{
144
- Set <String > contacts = getSharedPreferences ("conf" , MODE_PRIVATE ).getStringSet (getString (R .string .var_numbers_notify ), new HashSet <String >());
145
149
String keyword = getSharedPreferences ("conf" , MODE_PRIVATE ).getString (getString (R .string .var_words_keyword ), "Panic" );
146
150
SmsManager manager = SmsManager .getDefault ();
147
- for (String number : contacts )
151
+ for (Contact c : notifyContacts )
148
152
{
149
153
StringBuilder sb = new StringBuilder (keyword );
150
154
if (loc != null )
151
155
sb .append ("\n " + loc .getLatitude () + "\n " + loc .getLongitude ());
152
156
153
- manager .sendTextMessage (number . split ( ";" )[ 0 ] , null , sb .toString (), null , null );
157
+ manager .sendTextMessage (c . number , null , sb .toString (), null , null );
154
158
}
155
159
}
156
160
157
161
private void getCurrentLocationAndPanic ()
158
162
{
159
- if (callEmergServices ())
160
- {
161
- Intent emergService = new Intent (Intent .ACTION_CALL , Uri .parse ("tel:112" ));
162
- startActivity (emergService );
163
- return ;
164
- }
165
- Location currLoc ;
166
163
LocationManager locManager = (LocationManager ) getSystemService (LOCATION_SERVICE );
167
164
try
168
165
{
@@ -175,7 +172,7 @@ else if(locManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
175
172
}
176
173
catch (Exception e )
177
174
{
178
- Toast .makeText (this , "GPS fix could be acquired. Please check your settings!" , Toast .LENGTH_LONG ).show ();
175
+ Toast .makeText (this , "GPS fix could not be acquired. Please check your settings!" , Toast .LENGTH_LONG ).show ();
179
176
sendOutPanic (null );
180
177
}
181
178
}
@@ -205,4 +202,16 @@ public void onProviderEnabled(String provider) {
205
202
public void onProviderDisabled (String provider ) {
206
203
207
204
}
205
+
206
+ @ Override
207
+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
208
+ if (!Utils .onRequestPermissionsResult (requestCode , permissions , grantResults ))
209
+ {
210
+ new AlertDialog .Builder (TriggerActivity .this )
211
+ .setTitle ("Permissions" )
212
+ .setMessage ("It looks like not all permissions have been granted.\n Please grant them or the app will not work!" )
213
+ .show ();
214
+ findViewById (R .id .triggerButton ).setEnabled (false );
215
+ }
216
+ }
208
217
}
0 commit comments