From bf26676ec9f39f7a077cababf8dc225830a4d82d Mon Sep 17 00:00:00 2001 From: massimiliano Date: Wed, 22 May 2019 16:45:27 +0200 Subject: [PATCH] Notifications as service --- .idea/caches/build_file_checksums.ser | Bin 534 -> 534 bytes app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 7 + .../org/disroot/disrootapp/StatusService.java | 184 ++++++++++++++++++ .../disroot/disrootapp/ui/MainActivity.java | 177 +---------------- app/src/main/res/xml/changelog_master.xml | 3 + 6 files changed, 200 insertions(+), 175 deletions(-) create mode 100644 app/src/main/java/org/disroot/disrootapp/StatusService.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 07c88e644804024850df829678f57dc2013d0d5b..4d9f80324160804f74c144625da035ea1039d815 100644 GIT binary patch delta 15 XcmbQnGL2=z43_(;w`XmfQ^g1XG>-=2 delta 15 XcmbQnGL2=z43;A|Rc$xUsbT~GFwO=j diff --git a/app/build.gradle b/app/build.gradle index 5484be9..93ca716 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "org.disroot.disrootapp" minSdkVersion 15 targetSdkVersion 28 - versionCode 18 - versionName "1.1.5" + versionCode 19 + versionName "1.1.6" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ff5dcba..3e69325 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,11 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/DisTheme"> + + + + diff --git a/app/src/main/java/org/disroot/disrootapp/StatusService.java b/app/src/main/java/org/disroot/disrootapp/StatusService.java new file mode 100644 index 0000000..486b23d --- /dev/null +++ b/app/src/main/java/org/disroot/disrootapp/StatusService.java @@ -0,0 +1,184 @@ +package org.disroot.disrootapp; + +import android.annotation.SuppressLint; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.media.RingtoneManager; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Build; +import android.os.IBinder; +import android.support.v4.app.NotificationCompat; +import android.util.Log; +import android.widget.Toast; + +import org.disroot.disrootapp.ui.StateMessagesActivity; +import org.disroot.disrootapp.utils.HttpHandler; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Timer; +import java.util.TimerTask; + +import static android.support.constraint.motion.MotionScene.TAG; + +public class StatusService extends Service { + + //status report + public SharedPreferences checkDate; + // URL to get data JSON + static String incidenturl0 ="https://state.disroot.org/api/v1/incidents?sort=id&order=desc"; + ArrayList> messageList; + ArrayList> getDate; + + public StatusService() { + } + + @Override + public IBinder onBind(Intent intent) { + // TODO: Return the communication channel to the service. + throw new UnsupportedOperationException( "Not yet implemented" ); + + } + @Override + public void onCreate() { + super.onCreate(); + + //Status report + messageList = new ArrayList<>(); + getDate = new ArrayList<>(); + checkDate = getSharedPreferences("storeDate", Context.MODE_PRIVATE); + + //Check json for updates + Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + new StatusService.GetList().execute(); + } + }, 100, 100000);//100000=100sec + + + } + //status report + @SuppressLint("StaticFieldLeak") + class GetList extends AsyncTask { + + @Override + protected Void doInBackground(Void... arg0) { + HttpHandler sh = new HttpHandler(); + + String jsonStrincidents0 = sh.makeServiceCall(incidenturl0); + + Log.e(TAG, "Response from url(Service): " + incidenturl0); + + if (jsonStrincidents0 != null) {//Incidaetnts page + try { + JSONObject jsonObj = new JSONObject(jsonStrincidents0); + JSONArray data = jsonObj.getJSONArray("data"); + int a=0; + JSONObject o = data.getJSONObject(a); + String callid = o.getString("id"); + String updated = o.getString("updated_at"); + HashMap date = new HashMap<>(); + date.put("id", callid); + date.put("updated", updated); + getDate.add(date); + String stateDate = date.put( "updated", updated ); + String dateStored= checkDate.getString( "storeDate","" ); + + if (dateStored.equals( "" )) + { + checkDate.edit().putString( "storeDate", stateDate).apply(); + //return null; + } + else if (!stateDate.equals( dateStored )&& !stateDate.equals( "" ))//dateStored + { + checkDate.edit().putString( "storeDate", stateDate).apply(); + Log.e(TAG, "date: " + dateStored); + Log.e(TAG, "date2: " + stateDate); + sendNotification();//Call notification + return null; + } + else + Log.e(TAG, "updated json(service)"); + return null; + + } catch (final JSONException e) { + Log.e(TAG, "Json parsing error: " + e.getMessage()); + Toast.makeText(getApplicationContext(), + "Json parsing error: " + e.getMessage(), + Toast.LENGTH_LONG) + .show(); + } + }else { + Log.e(TAG, "Couldn't get json from server."); + } + return null; + } + } + + //Notification + private void sendNotification() throws JSONException { + String CHANNEL_ID = "3168654312"; + String CHANNEL_NAME = "StateNotification"; + HttpHandler sh = new HttpHandler(); + String jsonStrincidents0 = sh.makeServiceCall(incidenturl0); + JSONObject jsonObj = new JSONObject(jsonStrincidents0); + JSONArray data = jsonObj.getJSONArray("data"); + int a=0; + JSONObject o = data.getJSONObject(a); + String name = o.getString( "name" ); + String message = o.getString( "message" ); + HashMap date = new HashMap<>(); + date.put("name", name); + date.put("message", message); + Log.e(TAG, "message: " + name); + + Intent goState = new Intent( StatusService.this, StateMessagesActivity.class); + PendingIntent launchStateMessages = PendingIntent.getActivity(StatusService.this,0, goState, PendingIntent.FLAG_UPDATE_CURRENT); + + NotificationManager notificationManager = (NotificationManager) this.getSystemService( Context.NOTIFICATION_SERVICE); + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + + inboxStyle.addLine(message); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); + channel.enableVibration(true); + channel.setLightColor( Color.rgb( 80,22,45 )); + channel.enableLights(true); + channel.setVibrationPattern(new long[]{50,500,100,300,50,300}); + notificationManager.createNotificationChannel(channel); + } + + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) + .setAutoCancel( true ) + .setOngoing(true) + .setSmallIcon(R.drawable.ic_state) + .setContentTitle( getString( R.string.NotificationTitle ) ) + .setContentText(name)//get text Title from json :-) + .setContentInfo(message)//get text message from json :-) + .setContentIntent(launchStateMessages); + Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + notificationBuilder.setSound(alarmSound) + .setVibrate(new long[]{50,500,100,300,50,300}) + .setLights(Color.BLUE, 3000, 3000); + NotificationManager mNotificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + notificationBuilder.setChannelId(CHANNEL_ID); + } + + notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build()); + } +} diff --git a/app/src/main/java/org/disroot/disrootapp/ui/MainActivity.java b/app/src/main/java/org/disroot/disrootapp/ui/MainActivity.java index 41703ae..fa4e290 100644 --- a/app/src/main/java/org/disroot/disrootapp/ui/MainActivity.java +++ b/app/src/main/java/org/disroot/disrootapp/ui/MainActivity.java @@ -1,24 +1,16 @@ package org.disroot.disrootapp.ui; import android.Manifest; -import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.DownloadManager; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.ProgressDialog; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Color; -import android.media.RingtoneManager; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -30,7 +22,6 @@ import android.support.annotation.RequiresApi; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentManager; -import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; @@ -60,20 +51,15 @@ import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.FrameLayout; import android.widget.ImageButton; -import android.widget.ListView; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.Toast; import org.disroot.disrootapp.R; -//import org.disroot.disrootapp.service.CachetService; +import org.disroot.disrootapp.StatusService; import org.disroot.disrootapp.utils.Constants; -import org.disroot.disrootapp.utils.HttpHandler; import org.disroot.disrootapp.webviews.DisWebChromeClient; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import java.io.File; import java.io.IOException; @@ -84,9 +70,6 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; - import de.cketti.library.changelog.ChangeLog; @SuppressWarnings("ALL") @@ -121,15 +104,6 @@ public class MainActivity extends AppCompatActivity implements View.OnLongClickL private CookieManager cookieManager; - //status report - private ProgressDialog pDialog; - private ListView lv; - public SharedPreferences checkDate; - // URL to get data JSON - static String incidenturl0 ="https://state.disroot.org/api/v1/incidents?sort=id&order=desc"; - ArrayList> messageList; - ArrayList> getDate; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -668,34 +642,9 @@ public class MainActivity extends AppCompatActivity implements View.OnLongClickL } }); - //Status report - messageList = new ArrayList<>(); - getDate = new ArrayList<>(); - - lv = findViewById(R.id.list); - - checkDate = getSharedPreferences("storeDate", Context.MODE_PRIVATE); - - //Check json for updates - Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - runOnUiThread(new Runnable() { - - @Override - public void run() { - new MainActivity.GetList().execute(); - } - }); - } - }, 100, 100000);//100000=100sec - - // start CachetService - //Intent intent = new Intent(this, CachetService.class); - // Put some data for use by the IntentService - //intent.putExtra("foo", "bar"); - //startService(intent); + //Status service + Intent intent = new Intent( MainActivity.this, StatusService.class); + startService(intent); } @@ -1881,124 +1830,6 @@ public class MainActivity extends AppCompatActivity implements View.OnLongClickL startActivity(intent); } - //status report - @SuppressLint("StaticFieldLeak") - class GetList extends AsyncTask { - - @Override - protected Void doInBackground(Void... arg0) { - HttpHandler sh = new HttpHandler(); - - String jsonStrincidents0 = sh.makeServiceCall(incidenturl0); - - Log.e(TAG, "Response from url: " + incidenturl0); - - if (jsonStrincidents0 != null) {//Incidaetnts page - try { - JSONObject jsonObj = new JSONObject(jsonStrincidents0); - JSONArray data = jsonObj.getJSONArray("data"); - int a=0; - JSONObject o = data.getJSONObject(a); - String callid = o.getString("id"); - String updated = o.getString("updated_at"); - HashMap date = new HashMap<>(); - date.put("id", callid); - date.put("updated", updated); - getDate.add(date); - String stateDate = date.put( "updated", updated ); - String dateStored= checkDate.getString( "storeDate","" ); - - if (dateStored.equals( "" )) - { - checkDate.edit().putString( "storeDate", stateDate).apply(); - //return null; - } - else if (!stateDate.equals( dateStored )&& !stateDate.equals( "" ))//dateStored - { - checkDate.edit().putString( "storeDate", stateDate).apply(); - Log.e(TAG, "date: " + dateStored); - Log.e(TAG, "date2: " + stateDate); - sendNotification();//Call notification - return null; - } - else - Log.e(TAG, "updated json"); - return null; - - } catch (final JSONException e) { - Log.e(TAG, "Json parsing error: " + e.getMessage()); - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(getApplicationContext(), - "Json parsing error: " + e.getMessage(), - Toast.LENGTH_LONG) - .show(); - } - }); - } - }else { - Log.e(TAG, "Couldn't get json from server."); - } - return null; - } - } - - //Notification - private void sendNotification() throws JSONException { - String CHANNEL_ID = "3168654312"; - String CHANNEL_NAME = "StateNotification"; - HttpHandler sh = new HttpHandler(); - String jsonStrincidents0 = sh.makeServiceCall(incidenturl0); - JSONObject jsonObj = new JSONObject(jsonStrincidents0); - JSONArray data = jsonObj.getJSONArray("data"); - int a=0; - JSONObject o = data.getJSONObject(a); - String name = o.getString( "name" ); - String message = o.getString( "message" ); - HashMap date = new HashMap<>(); - date.put("name", name); - date.put("message", message); - Log.e(TAG, "message: " + name); - - Intent goState = new Intent(MainActivity.this, StateMessagesActivity.class); - PendingIntent launchStateMessages = PendingIntent.getActivity(MainActivity.this,0, goState, PendingIntent.FLAG_UPDATE_CURRENT); - - NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); - NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - - inboxStyle.addLine(message); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); - channel.enableVibration(true); - channel.setLightColor(Color.rgb( 80,22,45 )); - channel.enableLights(true); - channel.setVibrationPattern(new long[]{50,500,100,300,50,300}); - notificationManager.createNotificationChannel(channel); - } - - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) - .setAutoCancel( true ) - .setOngoing(true) - .setSmallIcon(R.drawable.ic_state) - .setContentTitle( getString( R.string.NotificationTitle ) ) - .setContentText(name)//get text Title from json :-) - .setContentInfo(message)//get text message from json :-) - .setContentIntent(launchStateMessages); - Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - notificationBuilder.setSound(alarmSound) - .setVibrate(new long[]{50,500,100,300,50,300}) - .setLights(Color.BLUE, 3000, 3000); - NotificationManager mNotificationManager = - (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ - notificationBuilder.setChannelId(CHANNEL_ID); - } - - notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build()); - } - //show snackbar to avoid exit on backpress @Override public void onBackPressed() { diff --git a/app/src/main/res/xml/changelog_master.xml b/app/src/main/res/xml/changelog_master.xml index 07610af..98a019b 100644 --- a/app/src/main/res/xml/changelog_master.xml +++ b/app/src/main/res/xml/changelog_master.xml @@ -1,5 +1,8 @@ + + Notifications works as a service now + Notifications works on Android 8 and higher now Added Disroot purple light notification