--- a +++ b/app/src/main/java/com/codesys/forge/MyService.java @@ -0,0 +1,195 @@ +package com.codesys.forge; + +import android.app.Notification; +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.os.AsyncTask; +import android.os.Binder; +import android.os.Build; +import android.os.IBinder; +import android.util.Log; +import android.webkit.CookieManager; +import android.widget.Toast; + +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Timer; +import java.util.TimerTask; + +/// +/// Background service +/// +public class MyService extends Service { + + private IBinder mBinder = (android.os.IBinder)new SocketServerBinder(); + private Timer mTimer; + private boolean mRunning = false; + private Context mContext; + + private void CreateNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CharSequence name = getString(R.string.channel_name); + String description = getString(R.string.channel_description); + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel channel = new NotificationChannel(getString(R.string.channel_id), name, importance); + channel.setDescription(description); + NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } + private void CreateNotification(int id) { + String title; + String description; + + switch(id) { + case 1: + title = getString(R.string.notification1_title); + description = getString(R.string.notification1_description); + break; + case 2: + title = getString(R.string.notification2_title); + description = getString(R.string.notification2_description); + break; + case 3: + title = getString(R.string.notification3_title); + description = getString(R.string.notification3_description); + break; + default: + title = "title undefined"; + description = "description undefined"; + break; + + } + + CreateNotificationChannel(); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + Intent intent = new Intent(this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.channel_id)) + .setSmallIcon(R.drawable.ic_codesys_logo) + .setContentTitle(title) + .setContentText(description) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setContentIntent(pendingIntent) + ; + notificationManager.notify(id, builder.build()); + } + protected void CheckForUpdatesAndNotify() { + new AsyncTask<Void, Void, String>() { + @Override + protected String doInBackground(Void... voids) { + String urlString = "https://forge.codesys.com/rest/forge/saml"; + String cookies = CookieManager.getInstance().getCookie(urlString); + try { + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setReadTimeout(10000 /* milliseconds */); + conn.setConnectTimeout(15000 /* milliseconds */); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + if (cookies != null) + conn.setRequestProperty("Cookie", cookies); + conn.connect(); + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + StringBuilder stringBuilder = new StringBuilder(); + + String line = null; + while ((line = reader.readLine()) != null) + { + stringBuilder.append(line + "\n"); + } + String news = stringBuilder.toString(); + JSONArray arr = new JSONArray(news); + for (int i=0; i < arr.length(); i++) + { + String uri = arr.getString(i); + switch(uri) { + case "/forge/projects": + CreateNotification(1); + break; + case "/forge/news": + CreateNotification(2); + break; + case "/forge/talk": + CreateNotification(3); + break; + } + } + return news; + } + catch(MalformedURLException e) { + + } + catch(IOException e) { + + } + catch(JSONException e) { + + } + return "error"; + } + + }.execute(); + + } + @Override + public void onCreate() { + mContext = this; + //Toast.makeText(mContext,"Service created",Toast.LENGTH_SHORT).show(); + super.onCreate(); + mTimer = new Timer(); + + mTimer.schedule(new TimerTask() { + + @Override + public void run() { + if (mRunning) { + CheckForUpdatesAndNotify(); + } + } + }, 10000, 3600000); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + mRunning = true; + return super.onStartCommand(intent, flags, startId); + } + + @Override + public IBinder onBind(Intent arg0) { + mRunning = true; + return mBinder; + } + + @Override + public boolean onUnbind(Intent intent) { + mRunning = false; + return super.onUnbind(intent); + } + + public class SocketServerBinder extends Binder { + + public MyService getService() { + return MyService.this; + } + + } + +}