Commit 5c904fa9 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca Committed by Pietro Saccardi

Fix some bugs - remove unused files

parent 84fcfedc
......@@ -31,6 +31,8 @@ dependencies {
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
......
......@@ -5,12 +5,14 @@
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
......@@ -42,11 +44,6 @@
android:label="Titolo..."
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
......
package it.giacomo.sesamo;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessaging;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.UUID;
import it.giacomo.sesamo.managers.SendSms;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
int mAppWidgetId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnEsci = (Button) findViewById(R.id.btnEsci);
FirebaseMessaging.getInstance().subscribeToTopic("OPEN");
btnEsci.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
String value = "";
//sono master!!!!
if(getIntent().getExtras() != null) {
value = getIntent().getExtras().getString("OPEN"); // lo faccio una volta per tutte!
boolean isFromFCM = getIntent().getExtras().getBoolean("IS_FROM_FCM");
if(isFromFCM)
{
SendIt(value);
return;
}
});
FirebaseMessaging.getInstance().subscribeToTopic("OPEN");
TextView mTextField = (TextView) findViewById(R.id.timer);
setContentView(R.layout.activity_main);
mTextField.setText("Benvenuto");
Button btnEsci = (Button) findViewById(R.id.btnEsci);
TextView mTextSummary = (TextView) findViewById(R.id.tvSummary);
btnEsci.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
}
});
if(getIntent().getExtras() != null) {
String value = getIntent().getExtras().getString("OPEN");
if(value.contains("OPEN")) { //meglio essere previdenti
SendSms sms = new SendSms();
TextView mTextField = (TextView) findViewById(R.id.timer);
String prefContatto = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("prefContatto", "");
TextView mTextSummary = (TextView) findViewById(R.id.tvSummary);
int prefOperatore = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("prefOperatore", "0"));
boolean prefMaster = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("prefMaster", false);
if(prefMaster) {
SendIt(value);
}
else
{
SendPost(value);
}
sms.Do(prefOperatore, prefContatto, null, value, null, null);
SetText(mTextSummary, value);
SetText(mTextSummary, value);
CountD(mTextField);
}
CountD(mTextField);
return;
}
setContentView(R.layout.activity_main);
Button btnEsci = (Button) findViewById(R.id.btnEsci);
btnEsci.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
}
});
}
private void SendIt(String value)
{
SendSms sms = new SendSms();
String prefContatto = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("prefContatto", "");
int prefOperatore = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("prefOperatore", "0"));
sms.Do(prefOperatore, prefContatto, null, value, null, null);
}
private void SetText(TextView mTextSummary, String function) {
......@@ -98,71 +137,63 @@ public class MainActivity extends AppCompatActivity {
private void sendSMS(final String phoneNumber, final String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
//-------------------- HTTP WAY -------------------------
private JSONObject SendPost(String message)
{
final String url = "https://fcm.googleapis.com/fcm/send";
JSONObject jsonObjectResp = null;
// {
// "to": "/topics/OPEN",
// "data" : {
// "body" : "OPEN_3",
// "priority" : "high",
// "UUID" : "FBFGBGBBGT"
// }
// }
JSONObject json = new JSONObject();
try {
json.put("to", "/topics/OPEN");
JSONObject jo = new JSONObject();
jo.put("body", message);
jo.put("priority", "high");
jo.put("UUID", UUID.randomUUID());
json.put("data", jo);
} catch (JSONException e) {
e.printStackTrace();
}
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
okhttp3.RequestBody body = RequestBody.create(JSON, json.toString());
okhttp3.Request request = new okhttp3.Request.Builder()
.url(url)
.post(body)
.addHeader("Authorization", "key=AAAAe1ej4BE:APA91bHt_SbgNw9Hgew90qpNcF9ioigkeJ2J-WQIui-jP5qMDxla1XQAXz0QbeBNd3doN7k0xbXOcdtBWYkvkcbRc75EBuS6ZUAwO4p3np3O_qqUPDkniYf1l5iPlxZzT1NeWt5ZgXA-")
//.addHeader("Content-Type", "application/json")
.build();
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
client.newCall(request).enqueue(new Callback() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
//for (int i = 0; i < MobNumber.size() - 1; i++) {
values.put("address", phoneNumber);// txtPhoneNo.getText().toString());
values.put("body", message);
//}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
public void onResponse(Call call, Response response) throws IOException {
String networkResp = response.body().string();
}
}, new IntentFilter(DELIVERED));
});
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
return jsonObjectResp;
}
}
......@@ -6,7 +6,6 @@ import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
* Created by Giacomo on 26/08/2017.
......@@ -19,26 +18,22 @@ public class MainActivityWidget extends AppWidgetProvider {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main_widget);
views.setOnClickPendingIntent(R.id.btn_esco, makePIntent(context, 0, "OPEN_0"));
views.setOnClickPendingIntent(R.id.btn_garage, makePIntent(context, 1, "OPEN_1"));
views.setOnClickPendingIntent(R.id.btn_portone, makePIntent(context, 2, "OPEN_2"));
views.setOnClickPendingIntent(R.id.btn_entro, makePIntent(context, 3, "OPEN_3"));
views.setOnClickPendingIntent(R.id.btn_esco, makePIntent(context, "OPEN_0"));
views.setOnClickPendingIntent(R.id.btn_garage, makePIntent(context, "OPEN_1"));
views.setOnClickPendingIntent(R.id.btn_portone, makePIntent(context, "OPEN_2"));
views.setOnClickPendingIntent(R.id.btn_entro, makePIntent(context, "OPEN_3"));
appWidgetManager.updateAppWidget(currentWidgetId, views);
Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();
}
}
private PendingIntent makePIntent(Context context, int value, String val)
public static PendingIntent makePIntent(Context context, String val)
{
Intent intent = new Intent(context, MainActivityWidget.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(val);
intent.putExtra("OPEN", val);
//PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
return PendingIntent.getBroadcast(context, 0, intent, 0);
//return pending;
}
@Override
......@@ -49,6 +44,7 @@ public class MainActivityWidget extends AppWidgetProvider {
Intent nextIntent = new Intent(context, MainActivity.class);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.putExtra("OPEN", intent.getAction());
nextIntent.putExtra("IS_FROM_WIDGET", true);
context.startActivity(nextIntent);
}
......
package it.giacomo.sesamo;
import android.*;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
......@@ -13,6 +15,7 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import java.util.List;
import java.util.SortedMap;
......@@ -108,8 +111,13 @@ public class SettingsActivity extends PreferenceActivity implements Preference.O
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getLoaderManager().initLoader(0, null, this);
if(AskForPermissions())
{
Init();
}
}
private void Init() {
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
Intent intent = getIntent();
......@@ -137,4 +145,54 @@ public class SettingsActivity extends PreferenceActivity implements Preference.O
}
return true;
}
//------------------- PERMISSIONS ---------------------
private static final int MY_PERMISSIONS_ALL = 5;
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
private boolean AskForPermissions()
{
String[] PERMISSIONS = { android.Manifest.permission.READ_CONTACTS, android.Manifest.permission.SEND_SMS, android.Manifest.permission.READ_PHONE_STATE };
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, MY_PERMISSIONS_ALL);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_ALL: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Init();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
......@@ -4,13 +4,17 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.annotation.BoolRes;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import it.giacomo.sesamo.MainActivity;
import it.giacomo.sesamo.MainActivityWidget;
import it.giacomo.sesamo.R;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
......@@ -21,6 +25,15 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String message = "";
try {
message = remoteMessage.getData().get("body");
}catch (Exception ex)
{
Toast.makeText(getApplicationContext(), "widget added", Toast.LENGTH_SHORT).show();
}
// ...
// TODO(developer): Handle FCM messages here.
......@@ -34,7 +47,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + message);
}
// Also if you intend on generating your own notifications as a result of a received FCM
......@@ -42,27 +55,22 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setSmallIcon(R.drawable.ic_vpn_key_black_24dp)
.setContentTitle("Sesamo")
.setContentText("OPEN :-)");
NextOperation(remoteMessage.getData().get("OPEN"));
Intent notificationIntent = new Intent(this, MainActivity.class);
// PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
private void NextOperation(String val)
{
Intent nextIntent = new Intent(getApplicationContext(), MainActivity.class);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.putExtra("OPEN", val);
boolean prefMaster = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("prefMaster", false);
getApplicationContext().startActivity(nextIntent);
if(prefMaster) {
Intent nextIntent = new Intent(getApplicationContext(), MainActivity.class);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
nextIntent.putExtra("OPEN", message);
nextIntent.putExtra("IS_FROM_FCM", true);
getApplicationContext().startActivity(nextIntent);
}
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M3,13h8L11,3L3,3v10zM3,21h8v-6L3,15v6zM13,21h8L21,11h-8v10zM13,3v6h8L21,3h-8z" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z" />
</vector>
<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="#FF000000"
android:pathData="M12.65,10C11.83,7.67 9.61,6 7,6c-3.31,0 -6,2.69 -6,6s2.69,6 6,6c2.61,0 4.83,-1.67 5.65,-4H17v4h4v-4h2v-4H12.65zM7,14c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2z"/>
</vector>
......@@ -13,7 +13,7 @@
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:gravity="center"
android:text="10"
android:text="Benvenuto"
android:fontFamily="sans-serif"
android:textSize="70dp"
android:layout_above="@+id/btnEsci"
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
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=".MainActivity"
android:transitionGroup="true"
android:orientation="horizontal">
android:gravity="center">
<Button
......@@ -24,6 +17,7 @@
android:id="@+id/btn_portone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/btn_garage"
android:text="PORTONE"
android:theme="@style/MyButton" />
......@@ -31,6 +25,7 @@
android:id="@+id/btn_esco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/btn_portone"
android:text="ESCO"
android:theme="@style/MyButton" />
......@@ -38,7 +33,8 @@
android:id="@+id/btn_entro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/btn_esco"
android:text="ENTRO"
android:theme="@style/MyButton" />
</LinearLayout>
\ No newline at end of file
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorWhite">#FFFFFF</color>
<color name="colorPrimary">#795548</color>
<color name="colorPrimaryDark">#5D4037</color>
<color name="primary_light">#D7CCC8</color>
<color name="colorAccent">#FF5722</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
......@@ -20,6 +20,6 @@
<style name="MyButton" > <!--<parent="Theme.AppCompat.Light"-->
<item name="colorControlHighlight">@color/colorPrimary</item>
<item name="colorButtonNormal">@color/colorAccent</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:textColor">@color/primary_text</item>
</style>
</resources>
......@@ -15,4 +15,13 @@
</PreferenceCategory>
<PreferenceCategory android:title="FGM notification">
<CheckBoxPreference
android:defaultValue="false"
android:key="prefMaster"
android:summary="Rendi questo telefono master"
android:title="Master" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment