|
| 1 | +package com.unixity.gagstock; |
| 2 | + |
| 3 | +import android.app.Notification; |
| 4 | +import android.app.NotificationChannel; |
| 5 | +import android.app.NotificationManager; |
| 6 | +import android.app.Service; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.SharedPreferences; |
| 10 | +import android.net.ConnectivityManager; |
| 11 | +import android.net.Network; |
| 12 | +import android.os.Build; |
| 13 | +import android.os.IBinder; |
| 14 | +import android.util.Log; |
| 15 | + |
| 16 | +import androidx.annotation.NonNull; |
| 17 | +import androidx.annotation.Nullable; |
| 18 | +import androidx.core.app.NotificationCompat; |
| 19 | + |
| 20 | +import org.json.JSONArray; |
| 21 | +import org.json.JSONObject; |
| 22 | + |
| 23 | +import okhttp3.OkHttpClient; |
| 24 | +import okhttp3.WebSocket; |
| 25 | +import okhttp3.WebSocketListener; |
| 26 | + |
| 27 | +public class BackgroundService extends Service { |
| 28 | + @Override |
| 29 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 30 | + startForeground(1, buildNotification()); |
| 31 | + |
| 32 | + ConnectivityManager connectivityManager = |
| 33 | + (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
| 34 | + |
| 35 | + if (connectivityManager != null) { |
| 36 | + connectivityManager.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback() { |
| 37 | + @Override |
| 38 | + public void onAvailable(@NonNull Network network) { |
| 39 | + Log.d("Network", "✅ Network is available. Connecting WebSocket..."); |
| 40 | + connectToEndpointInService(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onLost(@NonNull Network network) { |
| 45 | + Log.w("Network", "⚠️ Network connection lost. You may want to disconnect the WebSocket here."); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + return START_STICKY; |
| 51 | + } |
| 52 | + |
| 53 | + private void connectToEndpointInService() { |
| 54 | + SharedPreferences prefs = getApplicationContext().getSharedPreferences("notif_prefs", MODE_PRIVATE); |
| 55 | + OkHttpClient client = new OkHttpClient(); |
| 56 | + |
| 57 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 58 | + .url("wss://websocket.joshlei.com/growagarden/") |
| 59 | + .build(); |
| 60 | + |
| 61 | + client.newWebSocket(request, new WebSocketListener() { |
| 62 | + @Override |
| 63 | + public void onMessage(WebSocket webSocket, String text) { |
| 64 | + try { |
| 65 | + JSONObject json = new JSONObject(text); |
| 66 | + if (!json.has("seed_stock")) return; |
| 67 | + |
| 68 | + JSONArray seedStock = json.getJSONArray("seed_stock"); |
| 69 | + for (int i = 0; i < seedStock.length(); i++) { |
| 70 | + JSONObject seed = seedStock.getJSONObject(i); |
| 71 | + String rawName = seed.getString("display_name"); |
| 72 | + String key = rawName.toLowerCase().replaceAll("\\s+", ""); |
| 73 | + |
| 74 | + if (prefs.getBoolean(key, false)) { |
| 75 | + int quantity = seed.optInt("quantity", 0); |
| 76 | + sendSeedNotification(getApplicationContext(), rawName, quantity); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + Log.e("WebSocket", "Parsing error", e); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + private Notification buildNotification() { |
| 86 | + NotificationChannel channel = new NotificationChannel( |
| 87 | + "channel_id", "Background Channel", NotificationManager.IMPORTANCE_MIN); |
| 88 | + NotificationManager manager = getSystemService(NotificationManager.class); |
| 89 | + if (manager != null) manager.createNotificationChannel(channel); |
| 90 | +// i dont know how to make it non swipeable :sob: |
| 91 | + return new NotificationCompat.Builder(this, "channel_id") |
| 92 | + .setContentTitle("Service Running") |
| 93 | + .setContentText("Running in background") |
| 94 | + .setSmallIcon(R.mipmap.ic_launcher_foreground) |
| 95 | + .setPriority(NotificationCompat.PRIORITY_MIN) |
| 96 | + .setCategory(NotificationCompat.CATEGORY_SERVICE) |
| 97 | + .setOngoing(true) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + private void sendSeedNotification(Context context, String seedName, int quantity) { |
| 102 | + String channelId = "seed_channel"; |
| 103 | + |
| 104 | + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 105 | + NotificationChannel channel = new NotificationChannel( |
| 106 | + channelId, "Seed Stock Alerts", NotificationManager.IMPORTANCE_HIGH); |
| 107 | + manager.createNotificationChannel(channel); |
| 108 | + |
| 109 | + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) |
| 110 | + .setSmallIcon(R.mipmap.ic_launcher_foreground) |
| 111 | + .setContentTitle("Seed Restock Alert 🌱") |
| 112 | + .setContentText(seedName + " is back in stock! (Stock: x" + quantity + ")") |
| 113 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT) |
| 114 | + .setAutoCancel(true); |
| 115 | + manager.notify(seedName.hashCode(), builder.build()); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + @Override |
| 120 | + public IBinder onBind(Intent intent) { |
| 121 | + return null; // Not using binding |
| 122 | + } |
| 123 | +} |
0 commit comments