Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package group.ytos.u.Services;
- 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.content.SharedPreferences;
- import android.os.Build;
- import android.os.IBinder;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.app.NotificationManagerCompat;
- import android.support.v4.content.ContextCompat;
- import android.util.Log;
- import com.sholastik.notifications.ServiceBinder;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import group.ytos.u.Api;
- import group.ytos.u.FirstActivity;
- import group.ytos.u.GeneratedPV;
- import group.ytos.u.MainActivity;
- import group.ytos.u.Model.Auth.Resp;
- import group.ytos.u.Model.News.News;
- import group.ytos.u.Model.News.OneNews;
- import group.ytos.u.R;
- import group.ytos.u.RetrofitApi;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import static group.ytos.u.MainActivity.APP_PREFERENCES_COOKIE;
- import static group.ytos.u.MainActivity.APP_PREFERENCES_LOGIN_USER;
- import static group.ytos.u.MainActivity.APP_PREFERENCES_PASSWORD_USER;
- public class NewsService extends Service {
- private static final String LOG_TAG = "NewsServices";
- private static int NOTIFI;
- private static final String CHANNEL_ID = "News";
- private SharedPreferences preferences;
- private static String cookie;
- private MyThread mythread;
- public boolean isRunning = true;
- private Context context;
- private String login = "";
- private String password = "";
- private RetrofitApi RetrofitApi;
- private final ServiceBinder serviceBind = new ServiceBinder();
- public NewsService() {
- Log.d(LOG_TAG, "NewsService: ");
- }
- @Override
- public IBinder onBind(Intent intent) {
- serviceBind.onBind(this);
- return serviceBind;
- }
- public void forceForeground() {
- if (Build.VERSION.SDK_INT >= 26) {
- Intent intent = new Intent(this, NewsService.class);
- ContextCompat.startForegroundService(this, intent);
- mythread.run();
- }
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d(LOG_TAG, "onStartCommand: START");
- if (isRunning) {
- if (mythread.isAlive() != true)
- mythread.start();
- }
- return START_STICKY;
- }
- private class MyThread extends Thread {
- @Override
- public void run() {
- Log.d(LOG_TAG, "run: START");
- while (isRunning) {
- try {
- for (int i = 0; i < 30; i++) {
- TimeUnit.SECONDS.sleep(1);
- Log.d(LOG_TAG, "run: " + i);
- if (!isRunning) {
- Log.d(LOG_TAG, "run: exit");
- return;
- }
- }
- getNews();
- } catch (InterruptedException e) {
- isRunning = true;
- e.printStackTrace();
- }
- }
- }
- }
- @Override
- public void onCreate() {
- super.onCreate();
- Log.d(LOG_TAG, "onCreate: ");
- mythread = new MyThread();
- context = this;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- CharSequence name = "Новости";
- String description = "Категория в которой вы можете настроить уведомления о приходящих новостях с портала";
- int importance = NotificationManager.IMPORTANCE_DEFAULT;
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
- channel.setDescription(description);
- // Register the channel with the system; you can't change the importance
- // or other notification behaviors after this
- NotificationManager notificationManager = getSystemService(NotificationManager.class);
- notificationManager.createNotificationChannel(channel);
- }
- }
- private void getNews() {
- Log.d(LOG_TAG, "getNews: START");
- preferences = this.getSharedPreferences(MainActivity.APP_PREFERENCES, Context.MODE_PRIVATE);
- cookie = "";
- if (preferences.contains(APP_PREFERENCES_COOKIE)) {
- cookie = preferences.getString(APP_PREFERENCES_COOKIE, "");
- }
- if (cookie.isEmpty() || cookie == "")
- return;
- RetrofitApi RetrofitApi = new RetrofitApi();
- Api api = RetrofitApi.getApi();
- String pv = new GeneratedPV().getP_v();
- Call<News> call = api.getNews("id=" + cookie, pv);
- call.enqueue(new Callback<News>() {
- @Override
- public void onResponse(Call<News> call, Response<News> response) {
- if (response == null || response.body() == null || response.body().getOneNews() == null) {
- if (response.code() == 404) {
- Log.d(LOG_TAG, "onResponse: 404");
- } else {
- goAuth();
- }
- } else {
- List<OneNews> s = response.body().getOneNews();
- if (s != null) {
- for (int i = 0; i < s.size(); i++) {
- if (s.get(i).getViews().get(0).getCount().contains("0") || s.get(i).getViews().get(0).getCount().isEmpty()) {
- Intent intent = new Intent(context, FirstActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_notification_news)
- .setContentTitle(s.get(i).getHeader())
- .setContentText(s.get(i).getContent())
- .setContentIntent(pendingIntent)
- .setPriority(NotificationCompat.PRIORITY_DEFAULT)
- .setAutoCancel(true);
- NOTIFI = Integer.parseInt(s.get(i).getId());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- startForeground(NOTIFI, builder.build());
- } else {
- NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
- notificationManager.notify(NOTIFI, builder.build());
- }
- Log.d(LOG_TAG, "onResponse: notify created" + NOTIFI);
- NewsReaded(s.get(i).getId());
- }
- }
- } else {
- Log.d(LOG_TAG, "onResponse: LIST EMPTY");
- }
- }
- }
- @Override
- public void onFailure(Call<News> call, Throwable t) {
- Log.d(LOG_TAG, "onFailure: " + t.getMessage());
- }
- });
- }
- private void NewsReaded(final String id) {
- preferences = this.getSharedPreferences(MainActivity.APP_PREFERENCES, MODE_PRIVATE);
- if (preferences.contains(MainActivity.APP_PREFERENCES_COOKIE)) {
- cookie = preferences.getString(MainActivity.APP_PREFERENCES_COOKIE, "");
- }
- RetrofitApi RetrofitApi = new RetrofitApi();
- Api api = RetrofitApi.getApi();
- String xml = "<?xml version=\"1.0\" encoding=\"utf8\"?>" +
- "<Новости П_ПРОСМОТР_НВСТ_ИД=\"" + id + "\">" +
- "<Операция>ПРОСМОТР</Операция>" +
- "</Новости>";
- Call<String> call = api.setNewsReaded("id=" + cookie, xml);
- call.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- if (response == null || response.body() == null) {
- Log.d(LOG_TAG, "onResponse: news id - " + id + " NOT readed");
- } else {
- Log.d(LOG_TAG, "onResponse: news id - " + id + " readed");
- }
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- Log.d(LOG_TAG, "onFailure: " + t.getMessage());
- }
- });
- }
- private void goAuth() {
- Log.d(LOG_TAG, "goAuth: START");
- if (preferences.contains(APP_PREFERENCES_LOGIN_USER)) {
- login = preferences.getString(APP_PREFERENCES_LOGIN_USER, "");
- }
- if (preferences.contains(APP_PREFERENCES_PASSWORD_USER)) {
- password = preferences.getString(APP_PREFERENCES_PASSWORD_USER, "");
- }
- if (login.isEmpty() || password.isEmpty()) {
- return;
- }
- RetrofitApi = new RetrofitApi();
- Api api = RetrofitApi.getApi();
- Call<Resp> call = api.getResp(login, password);
- call.enqueue(new Callback<Resp>() {
- @Override
- public void onResponse(Call<Resp> call, Response<Resp> response) {
- String cookie = response.body().getCookie();
- SharedPreferences.Editor editor = preferences.edit();
- editor.putString(APP_PREFERENCES_COOKIE, cookie);
- editor.apply();
- }
- @Override
- public void onFailure(Call<Resp> call, Throwable t) {
- Log.e(LOG_TAG, "onFailure: " + t.getMessage());
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement