Advertisement
Guest User

Untitled

a guest
May 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. package group.ytos.u.Services;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.app.Service;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.os.Build;
  12. import android.os.IBinder;
  13. import android.support.v4.app.NotificationCompat;
  14. import android.support.v4.app.NotificationManagerCompat;
  15. import android.support.v4.content.ContextCompat;
  16. import android.util.Log;
  17.  
  18. import com.sholastik.notifications.ServiceBinder;
  19.  
  20. import java.util.List;
  21. import java.util.concurrent.TimeUnit;
  22.  
  23. import group.ytos.u.Api;
  24. import group.ytos.u.FirstActivity;
  25. import group.ytos.u.GeneratedPV;
  26. import group.ytos.u.MainActivity;
  27. import group.ytos.u.Model.Auth.Resp;
  28. import group.ytos.u.Model.News.News;
  29. import group.ytos.u.Model.News.OneNews;
  30. import group.ytos.u.R;
  31. import group.ytos.u.RetrofitApi;
  32. import retrofit2.Call;
  33. import retrofit2.Callback;
  34. import retrofit2.Response;
  35.  
  36. import static group.ytos.u.MainActivity.APP_PREFERENCES_COOKIE;
  37. import static group.ytos.u.MainActivity.APP_PREFERENCES_LOGIN_USER;
  38. import static group.ytos.u.MainActivity.APP_PREFERENCES_PASSWORD_USER;
  39.  
  40. public class NewsService extends Service {
  41.  
  42. private static final String LOG_TAG = "NewsServices";
  43. private static int NOTIFI;
  44. private static final String CHANNEL_ID = "News";
  45. private SharedPreferences preferences;
  46.  
  47. private static String cookie;
  48. private MyThread mythread;
  49. public boolean isRunning = true;
  50. private Context context;
  51.  
  52. private String login = "";
  53. private String password = "";
  54.  
  55. private RetrofitApi RetrofitApi;
  56.  
  57. private final ServiceBinder serviceBind = new ServiceBinder();
  58.  
  59. public NewsService() {
  60. Log.d(LOG_TAG, "NewsService: ");
  61. }
  62.  
  63. @Override
  64. public IBinder onBind(Intent intent) {
  65. serviceBind.onBind(this);
  66. return serviceBind;
  67. }
  68.  
  69. public void forceForeground() {
  70. if (Build.VERSION.SDK_INT >= 26) {
  71. Intent intent = new Intent(this, NewsService.class);
  72.  
  73. ContextCompat.startForegroundService(this, intent);
  74. mythread.run();
  75. }
  76. }
  77.  
  78. @Override
  79. public int onStartCommand(Intent intent, int flags, int startId) {
  80. Log.d(LOG_TAG, "onStartCommand: START");
  81.  
  82. if (isRunning) {
  83. if (mythread.isAlive() != true)
  84. mythread.start();
  85. }
  86. return START_STICKY;
  87. }
  88.  
  89. private class MyThread extends Thread {
  90.  
  91. @Override
  92. public void run() {
  93. Log.d(LOG_TAG, "run: START");
  94.  
  95. while (isRunning) {
  96. try {
  97.  
  98. for (int i = 0; i < 30; i++) {
  99. TimeUnit.SECONDS.sleep(1);
  100. Log.d(LOG_TAG, "run: " + i);
  101. if (!isRunning) {
  102. Log.d(LOG_TAG, "run: exit");
  103. return;
  104. }
  105. }
  106. getNews();
  107. } catch (InterruptedException e) {
  108. isRunning = true;
  109. e.printStackTrace();
  110. }
  111. }
  112. }
  113. }
  114.  
  115. @Override
  116. public void onCreate() {
  117. super.onCreate();
  118. Log.d(LOG_TAG, "onCreate: ");
  119.  
  120. mythread = new MyThread();
  121.  
  122. context = this;
  123.  
  124. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  125. CharSequence name = "Новости";
  126. String description = "Категория в которой вы можете настроить уведомления о приходящих новостях с портала";
  127. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  128. NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
  129. channel.setDescription(description);
  130. // Register the channel with the system; you can't change the importance
  131. // or other notification behaviors after this
  132. NotificationManager notificationManager = getSystemService(NotificationManager.class);
  133. notificationManager.createNotificationChannel(channel);
  134. }
  135. }
  136.  
  137. private void getNews() {
  138.  
  139. Log.d(LOG_TAG, "getNews: START");
  140.  
  141. preferences = this.getSharedPreferences(MainActivity.APP_PREFERENCES, Context.MODE_PRIVATE);
  142.  
  143. cookie = "";
  144.  
  145. if (preferences.contains(APP_PREFERENCES_COOKIE)) {
  146. cookie = preferences.getString(APP_PREFERENCES_COOKIE, "");
  147. }
  148.  
  149. if (cookie.isEmpty() || cookie == "")
  150. return;
  151.  
  152. RetrofitApi RetrofitApi = new RetrofitApi();
  153. Api api = RetrofitApi.getApi();
  154. String pv = new GeneratedPV().getP_v();
  155.  
  156. Call<News> call = api.getNews("id=" + cookie, pv);
  157.  
  158. call.enqueue(new Callback<News>() {
  159. @Override
  160. public void onResponse(Call<News> call, Response<News> response) {
  161.  
  162. if (response == null || response.body() == null || response.body().getOneNews() == null) {
  163. if (response.code() == 404) {
  164. Log.d(LOG_TAG, "onResponse: 404");
  165. } else {
  166. goAuth();
  167. }
  168. } else {
  169. List<OneNews> s = response.body().getOneNews();
  170. if (s != null) {
  171. for (int i = 0; i < s.size(); i++) {
  172.  
  173. if (s.get(i).getViews().get(0).getCount().contains("0") || s.get(i).getViews().get(0).getCount().isEmpty()) {
  174.  
  175. Intent intent = new Intent(context, FirstActivity.class);
  176. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  177. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  178.  
  179. NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
  180. .setSmallIcon(R.drawable.ic_notification_news)
  181. .setContentTitle(s.get(i).getHeader())
  182. .setContentText(s.get(i).getContent())
  183. .setContentIntent(pendingIntent)
  184. .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  185. .setAutoCancel(true);
  186.  
  187. NOTIFI = Integer.parseInt(s.get(i).getId());
  188.  
  189. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  190. startForeground(NOTIFI, builder.build());
  191. } else {
  192. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
  193. notificationManager.notify(NOTIFI, builder.build());
  194. }
  195.  
  196. Log.d(LOG_TAG, "onResponse: notify created" + NOTIFI);
  197.  
  198. NewsReaded(s.get(i).getId());
  199. }
  200. }
  201. } else {
  202. Log.d(LOG_TAG, "onResponse: LIST EMPTY");
  203. }
  204. }
  205. }
  206.  
  207. @Override
  208. public void onFailure(Call<News> call, Throwable t) {
  209. Log.d(LOG_TAG, "onFailure: " + t.getMessage());
  210. }
  211. });
  212. }
  213.  
  214. private void NewsReaded(final String id) {
  215. preferences = this.getSharedPreferences(MainActivity.APP_PREFERENCES, MODE_PRIVATE);
  216.  
  217. if (preferences.contains(MainActivity.APP_PREFERENCES_COOKIE)) {
  218. cookie = preferences.getString(MainActivity.APP_PREFERENCES_COOKIE, "");
  219. }
  220.  
  221. RetrofitApi RetrofitApi = new RetrofitApi();
  222. Api api = RetrofitApi.getApi();
  223.  
  224. String xml = "<?xml version=\"1.0\" encoding=\"utf8\"?>" +
  225. "<Новости П_ПРОСМОТР_НВСТ_ИД=\"" + id + "\">" +
  226. "<Операция>ПРОСМОТР</Операция>" +
  227. "</Новости>";
  228.  
  229. Call<String> call = api.setNewsReaded("id=" + cookie, xml);
  230.  
  231. call.enqueue(new Callback<String>() {
  232. @Override
  233. public void onResponse(Call<String> call, Response<String> response) {
  234.  
  235. if (response == null || response.body() == null) {
  236. Log.d(LOG_TAG, "onResponse: news id - " + id + " NOT readed");
  237. } else {
  238. Log.d(LOG_TAG, "onResponse: news id - " + id + " readed");
  239. }
  240. }
  241.  
  242. @Override
  243. public void onFailure(Call<String> call, Throwable t) {
  244. Log.d(LOG_TAG, "onFailure: " + t.getMessage());
  245. }
  246. });
  247. }
  248.  
  249. private void goAuth() {
  250. Log.d(LOG_TAG, "goAuth: START");
  251.  
  252. if (preferences.contains(APP_PREFERENCES_LOGIN_USER)) {
  253. login = preferences.getString(APP_PREFERENCES_LOGIN_USER, "");
  254. }
  255. if (preferences.contains(APP_PREFERENCES_PASSWORD_USER)) {
  256. password = preferences.getString(APP_PREFERENCES_PASSWORD_USER, "");
  257. }
  258.  
  259. if (login.isEmpty() || password.isEmpty()) {
  260. return;
  261. }
  262.  
  263. RetrofitApi = new RetrofitApi();
  264. Api api = RetrofitApi.getApi();
  265.  
  266. Call<Resp> call = api.getResp(login, password);
  267.  
  268. call.enqueue(new Callback<Resp>() {
  269.  
  270. @Override
  271. public void onResponse(Call<Resp> call, Response<Resp> response) {
  272.  
  273. String cookie = response.body().getCookie();
  274.  
  275. SharedPreferences.Editor editor = preferences.edit();
  276. editor.putString(APP_PREFERENCES_COOKIE, cookie);
  277. editor.apply();
  278. }
  279.  
  280. @Override
  281. public void onFailure(Call<Resp> call, Throwable t) {
  282. Log.e(LOG_TAG, "onFailure: " + t.getMessage());
  283. }
  284. });
  285.  
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement