Untitled
unknown
java
4 years ago
6.8 kB
5
Indexable
package ru.domyland.superdom.core.sip; 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.Build; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.socketio.client.IO; import com.github.nkzawa.socketio.client.Socket; import org.json.JSONException; import org.json.JSONObject; import java.net.URISyntaxException; import ru.domyland.superdom.R; import ru.domyland.superdom.core.MyApplication; import ru.domyland.superdom.data.api.API; import ru.domyland.superdom.data.db.User; import ru.domyland.superdom.presentation.activity.CallActivity; import ru.domyland.superdom.presentation.activity.WelcomeActivity; public class SipForegroundService extends Service { private NotificationManager notificationManager; public static final int DEFAULT_NOTIFICATION_ID = 101; private final String TAG = "SocketService"; private Socket mSocket; private User user; public SipForegroundService() { user = MyApplication.getInstance().getUser(); initSocket(); connect(); MyApplication.getInstance().setOnSocketUrlChanged(this::initSocket); } public void initSocket() { { try { Log.i(TAG, "connecting to " + MyApplication.getInstance().getSocketUrl()); // IO.Options options = new IO.Options(); // options.transports = new String[]{WebSocket.NAME}; // options.query = "token="+user.getToken(); mSocket = IO.socket(API.getSocketUrl()); } catch (URISyntaxException ignored) {} } } private final Emitter.Listener connectSocket = new Emitter.Listener() { @Override public void call(Object... args) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("Token", user.getToken()); } catch (JSONException e) { e.printStackTrace(); } mSocket.emit("authorization_req", jsonObject); } }; private final Emitter.Listener authorizationResult = args -> { for (Object obj : args) { Log.i(TAG, obj.toString()); } }; private final Emitter.Listener sipCall = args -> { if (args.length > 0) { String jsonData = args[0].toString(); Log.i(TAG, jsonData); } }; private final Emitter.Listener updateChat = args -> { Log.i(TAG, "chat message"); }; private final Emitter.Listener dis = args -> { for (int i = 0; i < args.length; i++) { Log.i(TAG, "DISCONNECT "+(i+1)+": "+args[i]); if (args[i].toString().equals("io client disconnect")) { connect(); } } }; private void initEmitters() { mSocket.on("connect", connectSocket); mSocket.on("sipcall", sipCall); mSocket.on("chat", updateChat); mSocket.on("authorization_res", authorizationResult); mSocket.on("disconnect", dis); mSocket.connect(); } public void connect() { initEmitters(); } public void disconnect() { mSocket.disconnect(); } public void onCreate() { super.onCreate(); notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { sendNotification(); return START_REDELIVER_INTENT; } public void sendNotification() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(); } else { Intent notificationIntent = new Intent(this, WelcomeActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(contentIntent) .setOngoing(true) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(getString(R.string.app_name)) .setContentText("В ожидании входящего вызова") .setWhen(System.currentTimeMillis()); Notification notification; notification = builder.build(); startForeground(DEFAULT_NOTIFICATION_ID, notification); } } @RequiresApi(Build.VERSION_CODES.O) private void createNotificationChannel() { Intent resultIntent = new Intent(this, WelcomeActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, resultIntent, 0); NotificationChannel channel = new NotificationChannel("my_service_tracker", "Входящие звонки", NotificationManager.IMPORTANCE_DEFAULT); channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "my_service_tracker"); assert manager != null; manager.createNotificationChannel(channel); Notification notification = notificationBuilder .setOngoing(true) .setSmallIcon(R.drawable.logo) .setContentTitle(getString(R.string.app_name)) .setContentText("В ожидании входящего вызова") .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(resultPendingIntent) .build(); startForeground(DEFAULT_NOTIFICATION_ID, notification); } @Override public void onDestroy() { super.onDestroy(); disconnect(); notificationManager.cancel(DEFAULT_NOTIFICATION_ID); stopSelf(); } }
Editor is loading...