[94fecf]: / app / src / main / java / com / codesys / forge / MyService.java  Maximize  Restore  History

Download this file

196 lines (169 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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/community-feed/":
                                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;
        }

    }

}