From 7aab6fbba93545d3662b3d34ff11a646e71deeaf Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 18 Sep 2020 14:37:34 +0200 Subject: [PATCH] Update info via a service --- app/src/acad/res/values/strings.xml | 1 + app/src/full/res/values/strings.xml | 1 + app/src/main/AndroidManifest.xml | 3 + .../fedilabtube/InstancePickerActivity.java | 15 +- .../app/fedilab/fedilabtube/MainActivity.java | 35 ++-- .../services/RetrieveInfoService.java | 95 ++++++++++ .../ic_notification_tubelab.xml | 16 ++ .../drawable-hdpi/ic_notification_tubelab.png | Bin 0 -> 499 bytes .../drawable-mdpi/ic_notification_tubelab.png | Bin 0 -> 359 bytes .../ic_notification_tubelab.png | Bin 0 -> 614 bytes .../ic_notification_tubelab.png | Bin 0 -> 905 bytes .../res/layout/activity_instance_picker.xml | 175 +++++++++--------- 12 files changed, 233 insertions(+), 108 deletions(-) create mode 100644 app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java create mode 100644 app/src/main/res/drawable-anydpi-v24/ic_notification_tubelab.xml create mode 100644 app/src/main/res/drawable-hdpi/ic_notification_tubelab.png create mode 100644 app/src/main/res/drawable-mdpi/ic_notification_tubelab.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_notification_tubelab.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_notification_tubelab.png diff --git a/app/src/acad/res/values/strings.xml b/app/src/acad/res/values/strings.xml index 3f0287b..502b19a 100644 --- a/app/src/acad/res/values/strings.xml +++ b/app/src/acad/res/values/strings.xml @@ -235,4 +235,5 @@ Display No opinion Pickup languages + Mise à jour des informations diff --git a/app/src/full/res/values/strings.xml b/app/src/full/res/values/strings.xml index ea7c315..2b30f00 100644 --- a/app/src/full/res/values/strings.xml +++ b/app/src/full/res/values/strings.xml @@ -251,4 +251,5 @@ Pickup categories Pickup languages + Update information \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e3b217a..574121e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -117,6 +117,9 @@ android:label="@string/about_the_app" android:windowSoftInputMode="stateAlwaysHidden" /> + diff --git a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java index e2a07a2..8049963 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java @@ -44,6 +44,7 @@ import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.entities.Instance; import app.fedilab.fedilabtube.client.entities.InstanceParams; import app.fedilab.fedilabtube.drawer.InstanceAdapter; +import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan; import app.fedilab.fedilabtube.viewmodel.InstancesVM; import es.dmoral.toasty.Toasty; @@ -160,6 +161,11 @@ public class InstancePickerActivity extends AppCompatActivity { } instanceParams.setLanguagesOr(langs); languages_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); + if (languages_view.getText().toString().trim().length() > 0) { + languages_view.setVisibility(View.VISIBLE); + } else { + languages_view.setVisibility(View.GONE); + } mainLoader.setVisibility(View.VISIBLE); viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); }); @@ -218,6 +224,11 @@ public class InstancePickerActivity extends AppCompatActivity { } instanceParams.setCategoriesOr(cats); categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); + if (categories_view.getText().toString().trim().length() > 0) { + categories_view.setVisibility(View.VISIBLE); + } else { + categories_view.setVisibility(View.GONE); + } mainLoader.setVisibility(View.VISIBLE); viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); }); @@ -257,11 +268,13 @@ public class InstancePickerActivity extends AppCompatActivity { return; } List instances = apiResponse.getInstances(); + RecyclerView lv_instances = findViewById(R.id.lv_instances); if ((instances == null || instances.size() == 0)) { textviewNoAction.setVisibility(View.VISIBLE); + lv_instances.setVisibility(View.GONE); } else { textviewNoAction.setVisibility(View.GONE); - RecyclerView lv_instances = findViewById(R.id.lv_instances); + lv_instances.setVisibility(View.VISIBLE); InstanceAdapter instanceAdapter = new InstanceAdapter(instances); lv_instances.setAdapter(instanceAdapter); lv_instances.setLayoutManager(new LinearLayoutManager(InstancePickerActivity.this)); diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 4a69fa8..37c06f9 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -20,6 +20,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; +import android.os.Build; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -42,14 +43,12 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; import org.jetbrains.annotations.NotNull; -import java.util.LinkedHashMap; - -import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo; import app.fedilab.fedilabtube.client.entities.PeertubeInformation; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.services.RetrieveInfoService; import app.fedilab.fedilabtube.sqlite.AccountDAO; import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.viewmodel.FeedsVM; @@ -67,6 +66,8 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + BottomNavigationView navView = findViewById(R.id.nav_view); if (Helper.isLoggedIn(MainActivity.this)) { @@ -75,23 +76,6 @@ public class MainActivity extends AppCompatActivity { navView.inflateMenu(R.menu.bottom_nav_menu); } - if (peertubeInformation == null) { - peertubeInformation = new PeertubeInformation(); - peertubeInformation.setCategories(new LinkedHashMap<>()); - peertubeInformation.setLanguages(new LinkedHashMap<>()); - peertubeInformation.setLicences(new LinkedHashMap<>()); - peertubeInformation.setPrivacies(new LinkedHashMap<>()); - peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); - peertubeInformation.setTranslations(new LinkedHashMap<>()); - new Thread(() -> { - try { - peertubeInformation = new PeertubeAPI(MainActivity.this).getPeertubeInformation(); - } catch (HttpsConnection.HttpsConnectionException e) { - e.printStackTrace(); - } - }).start(); - } - // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. AppBarConfiguration appBarConfiguration; @@ -107,6 +91,7 @@ public class MainActivity extends AppCompatActivity { .build(); } + startInForeground(); NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); if (navHostFragment != null) { NavInflater inflater = navHostFragment.getNavController().getNavInflater(); @@ -124,6 +109,16 @@ public class MainActivity extends AppCompatActivity { } } + + private void startInForeground() { + Intent notificationIntent = new Intent(this, RetrieveInfoService.class); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startForegroundService(notificationIntent); + } else { + startService(notificationIntent); + } + } + @Override public boolean onCreateOptionsMenu(@NotNull Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); diff --git a/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java b/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java new file mode 100644 index 0000000..65c9872 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/services/RetrieveInfoService.java @@ -0,0 +1,95 @@ +package app.fedilab.fedilabtube.services; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with TubeLab; if not, + * see . */ + +import android.app.Notification; +import android.app.Service; +import android.content.Intent; +import android.os.Build; +import android.os.IBinder; + +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; + +import java.util.LinkedHashMap; + +import app.fedilab.fedilabtube.R; +import app.fedilab.fedilabtube.client.HttpsConnection; +import app.fedilab.fedilabtube.client.PeertubeAPI; +import app.fedilab.fedilabtube.client.entities.PeertubeInformation; + +import static app.fedilab.fedilabtube.MainActivity.peertubeInformation; + + +public class RetrieveInfoService extends Service { + + static String NOTIFICATION_CHANNEL_ID = "update_info"; + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.ic_notification_tubelab) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.notification_channel_name)) + .setAutoCancel(true); + + Notification notification = builder.build(); + startForeground(1, notification); + + } else { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.notification_channel_name)) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setAutoCancel(true); + + Notification notification = builder.build(); + + startForeground(1, notification); + } + Thread thread = new Thread() { + + @Override + public void run() { + peertubeInformation = new PeertubeInformation(); + peertubeInformation.setCategories(new LinkedHashMap<>()); + peertubeInformation.setLanguages(new LinkedHashMap<>()); + peertubeInformation.setLicences(new LinkedHashMap<>()); + peertubeInformation.setPrivacies(new LinkedHashMap<>()); + peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); + peertubeInformation.setTranslations(new LinkedHashMap<>()); + try { + peertubeInformation = new PeertubeAPI(RetrieveInfoService.this).getPeertubeInformation(); + } catch (HttpsConnection.HttpsConnectionException e) { + e.printStackTrace(); + } + stopForeground(true); + + } + }; + thread.start(); + + return START_NOT_STICKY; + } + + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/app/src/main/res/drawable-anydpi-v24/ic_notification_tubelab.xml b/app/src/main/res/drawable-anydpi-v24/ic_notification_tubelab.xml new file mode 100644 index 0000000..4caaeda --- /dev/null +++ b/app/src/main/res/drawable-anydpi-v24/ic_notification_tubelab.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/app/src/main/res/drawable-hdpi/ic_notification_tubelab.png b/app/src/main/res/drawable-hdpi/ic_notification_tubelab.png new file mode 100644 index 0000000000000000000000000000000000000000..a58f6a58af4faaa51f1d0eac9d52e85cdd420c1b GIT binary patch literal 499 zcmVlPRN(U`7u14CBGos`DA~N zO)i)JBG%f@>4(#s(>v%5zIA7Un4G>XEpM>xjsVg+{j%k<`Ls{@g2a%1hUnXfKeVGA}1Kc5guSF3zR1_OAR@p85W$WROi_PreWY>;{i3N zbQ3!m0#qAwcp}bpvEUWuGi2$&a4&)g+ygn=0#E{-)lu9rR0aUhCOUQnL`r9(TtegE zkr1@6x%A+k`W-V=xrPEjHm8JedHTzSgC_>;1LGmNgvhTCk<3&AN-iPd(HH{sVgf{# z5C-z!u09aZQVM9?bASoZoDf5YO2$cQ!$E4UwqID<9t_sXi*c^F7*}d^=#0@}3P*>g zMn}nTT@D+rU?@?Rj#@wVB6Airb-;qAk{7hV3ac1bSXI;rsB}M|YL|SKT&l#~SHu5% z?r+c?byV4|4#8w#z4&x`jrd-{Mtl5a6$Lz;@&SB;YQk182>G&HiY$MrdNTS((MbxL$j z2?A@|m4tY^tV!y0+OZ4*XJaImxk9MZSzka9l#IsSK_GJpY9!(biBxTqRZWxmsWD*! zS#V8X{-Ei^pz(r1)2BkbDXe`e_TW=N9=o-M#7n+LW%Q&vK9WpX=o6pyC#>Gn@Bs{f zCt^U~M&j1&?-S(`1RBPaj>002ovPDHLk FV1ij5nLPji literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_notification_tubelab.png b/app/src/main/res/drawable-xhdpi/ic_notification_tubelab.png new file mode 100644 index 0000000000000000000000000000000000000000..347f385cf498d520d1da9fbc6194f34af7b49eb8 GIT binary patch literal 614 zcmV-s0-61ZP)N22C%_p1K6P1plm=kC>ziX+6{84f}9Y zJGN64A9(}OT31GoMq8tIjmrqXweW293c!ymfPG}PJ#Gd3sp;gIg^1atHri`Gnb)ql zz@Iz-Oh%Q_t5IRJ0Tu9_f@fTEq6XhOKue>Cfb|Ywvqq-P0bZtL6UbhYjl>H02W1)r zA`em$#_()e@Mg4Rh_MpZ#^fB6ZBUoVkm+nLRfjBsrOI2za|U}8y{~a&@%b_%fzlEa zjgK*Q!d3xsvm*@bew!5ulIst5v0p;i*Sg};a0J>-?izzqdb zLhl&fLgni|heQH8)uo{9aJv!0bc)zaIBNegl7!ncpi=xPikLvOOL<{KHoGzqh;k_? z#ee+~J_H0KfgqQ{*7YIKFca{0DXg%36#^3{0d5yk+%Ci!#3C_>r>)ZH#F<8=GgYM# zP}~O@XDK0yNC_z=IEuL`TQO(xwK1WbRz&6W=-t{9HZ-Cjkl=;}jD{C(DTmmmzTcp! z@1v^&%S52dNT9LuH-p#*thWc7ksZ88Okl0zW$t_7TIvL!F9A22XRZuUuUte>$b;>Y4>{}xLf}J8 z3rj^rB>=x|0^E{K8VGjVRqApDE8*Fy_{bZ`H&LQBEX13?S^xk507*qoM6N<$g19dZ AO#lD@ literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_notification_tubelab.png b/app/src/main/res/drawable-xxhdpi/ic_notification_tubelab.png new file mode 100644 index 0000000000000000000000000000000000000000..f928b4f3122f927e9a64da3ba1aabaaacb5759c6 GIT binary patch literal 905 zcmV;419tq0P)HV7NQ2F(V^2C#u^diwAj*VwWx%Yvdie`>MC zFV^30GFf)oX{Q~@lu~cHTy(kWGAg@`@W1ObZ$|`@>++$?P0`s6>df~Hrh;ESwykYc zdjm5nej7!D(#{pcVH6U6g)}&;fX+&et-f5O2yjR^ExJs)^t!a0(!<}Dz_80x!6`j@ z%>pl8yEqO%L<7$*9=)kdA;d0lexMMxUqhWPkSZ=XjU$*M^1$E%s^RE47`t;0x!~6w z>06arbGn3sMwdjBKqO~`0M}V!1AST$kaEgKM+BTQ6JpTYZIDK)#@ao>afH-Tj1u$S z;5a~ibcsVH~bka6E|BAd#@@n!phbVo|2q`+d)|(!jV(X75Ao7qi=+NXN(31c_q#NtQl$K?_`)&|7=(h2xXBmnr`nwx|BN8U&0mtC<#A>j;^4d%$?Y$=K4HaJi6Yf{L(W7dqZ zUwr&T?h=%-9Kc>?c_g^Z@<94l#&*W#I@FcnI@DFnb*RlRwm5=%Cv5*+xm_UUzY*Y| zNn0UbVSOmKE13m6b5-uwK%dD*SQy`2tgpS{vMD$CTgYwdK}q;J!+)!DDYlSJ z0*M845CCUT(mpKki-1oFz)Wg~-|*-e!1TKU^FqiQ!th(5u14+o^Ej5Ch5huz_R|$r fcG_vD9m&1{2 - -