--- a/app/src/main/java/com/codesys/forge/MainActivity.java +++ b/app/src/main/java/com/codesys/forge/MainActivity.java @@ -4,6 +4,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.Service; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Intent; @@ -11,6 +12,7 @@ import android.content.Context; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import android.app.ProgressDialog; @@ -148,6 +150,7 @@ refreshLayout.setRefreshing(true); currentURL = Url; myWebView.loadUrl(Url); + RemoveNotification(); } private boolean getNetworkState() { @@ -171,24 +174,6 @@ private boolean getOffline() { return xOffline; - } - - protected void notification() { - NotificationManager manager = (NotificationManager) - getSystemService(NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel("___$CODEYSForge/$__", - "CODESYS Forge Channel", - NotificationManager.IMPORTANCE_MIN); - manager.createNotificationChannel(channel); - } - Notification notification = new NotificationCompat.Builder(this, "CODESYS Forge") - .setContentTitle("CODEYS Forge") - .setSmallIcon(R.mipmap.ic_launcher_foreground) - .setPriority(NotificationCompat.PRIORITY_MIN) - .setOngoing(true) - .build(); - manager.notify(0, notification); } protected boolean isLoggedIn() { @@ -199,72 +184,13 @@ return false; } - protected void getNews() { - 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/news": - xNews = true; - break; - case "/forge/projects": - xProjects = true; - break; - case "/forge/talk": - xTalk = true; - break; - } - } - return news; - } - catch(MalformedURLException e) { - - } - catch(IOException e) { - - } - catch(JSONException e) { - - } - - return "error"; - } - - }.execute(); - - } - @Override public void onRefresh() { refreshLayout.setRefreshing(true); setOnline(); ReLoadWebView(currentURL); } + private void ReLoadWebView(String currentURL) { if(getNetworkState()) { setOnline(); @@ -273,8 +199,72 @@ { setOffline(); } - myWebView.loadUrl(currentURL); + RemoveNotification(); + } + 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 RemoveNotification() { + int id = 0; + if (currentURL.contains("forge/community-feed")) { + id = 1; + } + else if (currentURL.contains("forge/news")) { + id = 2; + } + else if (currentURL.contains("forge/talk")) { + id = 3; + } + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + notificationManager.cancel(id); + } + + 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(); + 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); + + + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + + notificationManager.notify(id, builder.build()); + } @Override @@ -282,6 +272,10 @@ String myUseragent; super.onCreate(savedInstanceState); + + // start long polling service + startService(new Intent(this, MyService.class)); + myContext = getApplicationContext(); // @@ -521,21 +515,21 @@ public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch(menuItem.getItemId()) { + case R.id.community: + loadUrl("https://forge.codesys.com/forge/community-feed", "Community"); + break; case R.id.news: loadUrl("https://forge.codesys.com/forge/news", "News"); - break; - case R.id.community: - loadUrl("https://forge.codesys.com/forge/community-feed", "Community"); break; case R.id.talk: loadUrl("https://forge.codesys.com/forge/talk", "Talk"); break; } menuItem.setChecked(true); -// getNews(); -// notification(); return true; } }; } + +