|
@@ -0,0 +1,231 @@
|
|
|
+package com.vpn.fastestvpnservice.fcm;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.app.job.JobInfo;
|
|
|
+import android.app.job.JobParameters;
|
|
|
+import android.app.job.JobScheduler;
|
|
|
+import android.app.job.JobService;
|
|
|
+import android.content.ComponentName;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.media.RingtoneManager;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
+import android.net.Uri;
|
|
|
+import android.net.wifi.ScanResult;
|
|
|
+import android.net.wifi.SupplicantState;
|
|
|
+import android.net.wifi.WifiInfo;
|
|
|
+import android.net.wifi.WifiManager;
|
|
|
+import android.os.Build;
|
|
|
+import android.util.Log;
|
|
|
+import androidx.core.app.ActivityCompat;
|
|
|
+import androidx.core.app.NotificationCompat;
|
|
|
+
|
|
|
+import com.vpn.fastestvpnservice.MainActivity;
|
|
|
+import com.vpn.fastestvpnservice.R;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
+import de.blinkt.openvpn.core.App;
|
|
|
+
|
|
|
+public class NetworkJobService extends JobService {
|
|
|
+
|
|
|
+ private WifiManager wifiManager;
|
|
|
+ public Context context = null;
|
|
|
+ Timer timer;
|
|
|
+ private static final int WIFI_SCAN_INTERVAL = 60 * 1000; // 60 sec
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onStartJob(JobParameters jobParameters) {
|
|
|
+ Log.d("NetworkJobService", "Job started");
|
|
|
+
|
|
|
+ wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ if (wifiManager != null) {
|
|
|
+ wifiManager.startScan();
|
|
|
+ }
|
|
|
+
|
|
|
+// handleScanResults(jobParameters);
|
|
|
+
|
|
|
+ timer = new Timer();
|
|
|
+ timer.scheduleAtFixedRate(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Log.d("NetworkJobService", "Job handleScanResults");
|
|
|
+
|
|
|
+ handleScanResults(jobParameters);
|
|
|
+ }
|
|
|
+ }, 10000, WIFI_SCAN_INTERVAL);
|
|
|
+
|
|
|
+// jobFinished(jobParameters, false);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleScanResults(JobParameters jobParameters) {
|
|
|
+ String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION};
|
|
|
+ Boolean isOpenWifiConnected = false;
|
|
|
+
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ // TODO: Consider calling
|
|
|
+ // ActivityCompat#requestPermissions
|
|
|
+ // here to request the missing permissions, and then overriding
|
|
|
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
+ // int[] grantResults)
|
|
|
+ // to handle the case where the user grants the permission. See the documentation
|
|
|
+ // for ActivityCompat#requestPermissions for more details.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<ScanResult> scanResults = wifiManager.getScanResults();
|
|
|
+ Log.d("NetworkJobService", "Job scanResults + " + scanResults.size());
|
|
|
+
|
|
|
+ List<ScanResult> nonZeroList = new ArrayList<>();
|
|
|
+
|
|
|
+ if (scanResults.size() > 0) {
|
|
|
+ Log.d("nonZeroList_test 1", scanResults.size() + "");
|
|
|
+
|
|
|
+ nonZeroList.addAll(scanResults);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+// Log.d("nonZeroList test 2", WiFiMonitorService.scanResultsStatic.size() + "");
|
|
|
+ Log.d("nonZeroList_test 2", WiFiMonitorService.scanResultsStatic.size() + "");
|
|
|
+
|
|
|
+// nonZeroList.addAll(WiFiMonitorService.scanResultsStatic);
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("NetworkJobService Start", "Started");
|
|
|
+
|
|
|
+ Log.d("NetworkJobService in", "Inside");
|
|
|
+
|
|
|
+ WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+
|
|
|
+
|
|
|
+ if (wifiManager.isWifiEnabled()) {
|
|
|
+
|
|
|
+ Log.d("NetworkJobService res", scanResults.size() + "isWifiEnabled()");
|
|
|
+
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
|
|
|
+
|
|
|
+ if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
|
|
|
+ WifiManager wifiManager1 = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ WifiInfo wifiInfo = wifiManager1.getConnectionInfo();
|
|
|
+
|
|
|
+ if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
|
|
|
+ String connectedSSID = wifiInfo.getSSID().replace("\"", "");
|
|
|
+
|
|
|
+ Log.d("NetworkJobService cssid", connectedSSID);
|
|
|
+
|
|
|
+ for (ScanResult scanResult : nonZeroList) {
|
|
|
+
|
|
|
+ Log.d("NetworkJobService list", scanResult.SSID);
|
|
|
+
|
|
|
+ String ssid = scanResult.SSID;
|
|
|
+
|
|
|
+ if (scanResult.capabilities != null) {
|
|
|
+ if (scanResult.capabilities.equals("[ESS]")) {
|
|
|
+ Log.d("NetworkJobService ESS", "Open wifi detected " + ssid);
|
|
|
+
|
|
|
+ String targetSSID = ssid;
|
|
|
+ Log.d("NetworkJobService Conn", "Normal wifi connected " + connectedSSID);
|
|
|
+
|
|
|
+ if (connectedSSID.equals(targetSSID)) {
|
|
|
+ Log.d("NetworkJobService con", "Device is connected to the open Wi-Fi network: " + connectedSSID);
|
|
|
+ sendNotification("Your Wi-Fi connection is unsafe", "Connect to VPN to protect your data");
|
|
|
+// jobFinished(jobParameters, false); // now set to true
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ Log.d("NetworkJobService conn", "Device is connected to the Normal Wi-Fi network: " + connectedSSID);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ jobFinished(jobParameters, true); // set to true for continuously, otherwise will set to false for 15 min each job
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onStopJob(JobParameters jobParameters) {
|
|
|
+ Log.d("NetworkJobService Stop", "Job Stop");
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void scheduleJob(Context context) {
|
|
|
+ Log.d("NetworkJobService scdul", "Job scheduleJob");
|
|
|
+ JobScheduler jobScheduler = (JobScheduler) context.getApplicationContext().getSystemService(JOB_SCHEDULER_SERVICE);
|
|
|
+ if (jobScheduler != null) {
|
|
|
+ ComponentName componentName = new ComponentName(context.getApplicationContext(), NetworkJobService.class);
|
|
|
+ JobInfo jobInfo = new JobInfo.Builder(1, componentName)
|
|
|
+ .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
|
|
+ .setRequiresCharging(false)
|
|
|
+ .setPeriodic(15 * 60 * 1000) // Job will be run every 15 minutes (minimum interval)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ jobScheduler.schedule(jobInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendNotification(String title, String messageBody) {
|
|
|
+ Intent intent = new Intent(getApplicationContext(), MainActivity.class);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
|
|
|
+// PendingIntent.FLAG_ONE_SHOT);
|
|
|
+// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
|
|
|
+// PendingIntent.FLAG_IMMUTABLE);
|
|
|
+ PendingIntent pendingIntent = null;
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
+ pendingIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis() /* Request code */, intent,
|
|
|
+ PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ }
|
|
|
+// else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+// pendingIntent = PendingIntent.getForegroundService(this, 0, intent,
|
|
|
+// PendingIntent.FLAG_IMMUTABLE);
|
|
|
+// }
|
|
|
+ else{
|
|
|
+ pendingIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis() /* Request code */, intent,
|
|
|
+ PendingIntent.FLAG_IMMUTABLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ String channelId = getApplicationContext().getString(R.string.default_notification_channel_id);
|
|
|
+ Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
+
|
|
|
+ NotificationManager notificationManager =
|
|
|
+ (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+
|
|
|
+ // Since android Oreo notification channel is needed.
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationChannel channel = new NotificationChannel(channelId,
|
|
|
+ "FastestVPN",
|
|
|
+ NotificationManager.IMPORTANCE_HIGH);
|
|
|
+ notificationManager.createNotificationChannel(channel);
|
|
|
+ }
|
|
|
+
|
|
|
+ NotificationCompat.Builder notificationBuilder =
|
|
|
+ new NotificationCompat.Builder(getApplicationContext(), channelId)
|
|
|
+// .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo))
|
|
|
+ .setSmallIcon(R.drawable.ic_logo_notify)
|
|
|
+ .setColor(getApplicationContext().getResources().getColor(R.color.app_yellow_color))
|
|
|
+// .setContentTitle("New Message")
|
|
|
+ .setContentTitle(title)
|
|
|
+ .setContentText(messageBody)
|
|
|
+ .setAutoCancel(false)
|
|
|
+ .setSound(defaultSoundUri)
|
|
|
+ .setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
|
+ .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
+ .setContentIntent(pendingIntent);
|
|
|
+
|
|
|
+
|
|
|
+ notificationManager.notify(App.NOTIFICATION_ID /* ID of notification */, notificationBuilder.build());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|