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

Download this file

207 lines (185 with data), 7.5 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
196
197
198
199
200
201
202
203
204
205
206
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 org.json.JSONObject;

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;
    String cookies = "";

    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 url;
        String title;
        String description;

        switch(id) {
            case 1:
                url = "https://forge.codesys.com/forge/community-feed";
                title = getString(R.string.notification1_title);
                description = getString(R.string.notification1_description);
                break;
            case 2:
                url = "https://forge.codesys.com/forge/news";
                title = getString(R.string.notification2_title);
                description = getString(R.string.notification2_description);
                break;
            case 3:
                url = "https://forge.codesys.com/forge/talk";
                title = getString(R.string.notification3_title);
                description = getString(R.string.notification3_description);
                break;
            case 4:
                url = "https://forge.codesys.com/forge/product-news";
                title = getString(R.string.notification4_title);
                description = getString(R.string.notification4_description);
                break;
            default:
                url = "url undefined";
                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);
        intent.putExtra("url", url);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        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() {
            String urlString = "https://forge.codesys.com/rest/forge/saml";
            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();
                JSONObject obj = new JSONObject(news);
                for (int i=0; i < obj.getJSONArray("news").length(); i++)
                {
                    String uri = obj.getJSONArray("news").getString(i);
                    switch(uri) {
                        case "/forge/community-feed/":
                            CreateNotification(1);
                            break;
                        case "/forge/news/":
                            CreateNotification(2);
                            break;
                        case "/forge/talk/":
                            CreateNotification(3);
                            break;
                        case "/forge/product-news/":
                            CreateNotification(4);
                            break;
                    }
                }
            }
            catch(MalformedURLException e) {
                Log.e("get news", e.getLocalizedMessage());
            }
            catch(IOException e) {
                Log.e("get news", e.getLocalizedMessage());
            }
            catch(JSONException e) {
                Log.e("get news", e.getLocalizedMessage());
            }
    }
    @Override
    public void onCreate() {
        mContext = this;
        //Toast.makeText(mContext,"Service created",Toast.LENGTH_SHORT).show();
        //CreateNotification(2);
        super.onCreate();
        mTimer = new Timer();

        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                if (mRunning) {
                    CheckForUpdatesAndNotify();
                }
            }
        }, 0, 3600000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mRunning = true;
        if (intent != null) {
            cookies = intent.getStringExtra("cookies");
        }
        //return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @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;
        }

    }

}