Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
bug floating window
Browse files Browse the repository at this point in the history
  • Loading branch information
esalessandrxx committed Jan 20, 2024
1 parent c5f463a commit 4971b9b
Show file tree
Hide file tree
Showing 22 changed files with 698 additions and 482 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "com.arr.simple"
minSdk = 23
targetSdk = 34
versionCode = 47
versionName = "4.0.7-beta"
versionCode = 50
versionName = "5.0-beta"

vectorDrawables {
useSupportLibrary = true
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.SCHEDULE_EXACT_ALARM"
android:maxSdkVersion="31" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"
android:minSdkVersion="34" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand All @@ -33,7 +39,6 @@
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
Expand Down Expand Up @@ -87,9 +92,9 @@
</intent-filter>
</receiver>
<service
android:name=".services.TrafficFloatingWindow" />
<service
android:name=".services.FloatingWindow" />
android:name=".services.TrafficFloatingWindow"
android:foregroundServiceType="connectedDevice" />

<receiver
android:name=".service.network"
android:exported="false">
Expand Down
43 changes: 27 additions & 16 deletions app/src/main/java/com/arr/simple/adapter/BalancesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.arr.simple.databinding.LayoutViewItemsBonosBalanceBinding;
import com.arr.simple.model.Balances;
import java.util.List;
import com.arr.simple.R;

public class BalancesAdapter extends PagerAdapter {
public class BalancesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context mContext;
private List<Balances> list;
Expand All @@ -21,25 +20,37 @@ public BalancesAdapter(Context context, List<Balances> list) {
}

@Override
public int getCount() {
public int getItemCount() {
return list.size();
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object arg1) {
return view == arg1;
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof Bonos) {
Balances model = list.get(position);
Bonos view = (Bonos) holder;

view.binding.icon.setImageResource(model.getIcon());
view.binding.typeBono.setText(model.getTitle());
view.binding.expire.setText(model.getVence());
}
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.slider_item_bonos, container, false);
return view;
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutViewItemsBonosBalanceBinding view =
LayoutViewItemsBonosBalanceBinding.inflate(
LayoutInflater.from(parent.getContext()), parent, false);
return new Bonos(view);
}

@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
class Bonos extends RecyclerView.ViewHolder {

private LayoutViewItemsBonosBalanceBinding binding;

public Bonos(LayoutViewItemsBonosBalanceBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
9 changes: 2 additions & 7 deletions app/src/main/java/com/arr/simple/model/Balances.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

public class Balances {

private String title, balance, vencimiento;
private String title, vencimiento;
private int icon;

public Balances(String title, String balance, String vencimiento, int icon) {
public Balances(String title, String vencimiento, int icon) {
this.title = title;
this.balance = balance;
this.vencimiento = vencimiento;
this.icon = icon;
}
Expand All @@ -16,10 +15,6 @@ public String getTitle() {
return title;
}

public String getBalance() {
return balance;
}

public String getVence() {
return vencimiento;
}
Expand Down
118 changes: 85 additions & 33 deletions app/src/main/java/com/arr/simple/services/TrafficFloatingWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.GradientDrawable;
import android.net.ConnectivityManager;
Expand All @@ -19,17 +21,20 @@
import android.os.IBinder;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import android.widget.LinearLayout;
import androidx.core.app.NotificationCompat;

import androidx.preference.PreferenceManager;
import com.arr.simple.R;
import com.arr.simple.databinding.LayoutFloatingWindowBinding;
import com.arr.simple.databinding.LayoutViewDeleteServiceBinding;

public class TrafficFloatingWindow extends Service {

Expand All @@ -39,7 +44,10 @@ public class TrafficFloatingWindow extends Service {
private static final String CHANNEL = "traffic";
private LayoutFloatingWindowBinding binding;
private BroadcastReceiver broadcastNetwork;


private LayoutViewDeleteServiceBinding delete;
private WindowManager.LayoutParams lParamsDelete;

@Override
public IBinder onBind(Intent intent) {
return null;
Expand Down Expand Up @@ -68,9 +76,8 @@ public void run() {

handler.postDelayed(runnable, 1000);
}


public String calculateSpeed(long timeTaken, long downBytes, long upBytes) {

public String calculateSpeed(long timeTaken, long downBytes, long upBytes) {
long downSpeed = 0;
long upSpeed = 0;

Expand Down Expand Up @@ -114,10 +121,10 @@ public void onCreate() {
lastRxBytes = TrafficStats.getTotalRxBytes();
lastTxBytes = TrafficStats.getTotalTxBytes();
lastTime = System.currentTimeMillis();


windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
binding = LayoutFloatingWindowBinding.inflate(LayoutInflater.from(this));
delete = LayoutViewDeleteServiceBinding.inflate(LayoutInflater.from(this));

SharedPreferences spColor = PreferenceManager.getDefaultSharedPreferences(this);
String selectColor = spColor.getString("floating_color", "colorPrimary");
Expand All @@ -126,7 +133,7 @@ public void onCreate() {
.getColor(
getResources()
.getIdentifier(selectColor, "color", getPackageName()));
GradientDrawable drawable = (GradientDrawable) binding.floating.getBackground();
GradientDrawable drawable = (GradientDrawable) binding.back.getBackground();
drawable.setColor(color);

//
Expand All @@ -137,13 +144,27 @@ public void onCreate() {
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);

lParamsDelete =
new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);
} else {
layoutParams =
new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);

lParamsDelete =
new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);
}
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = 0;
Expand All @@ -153,9 +174,18 @@ public void onCreate() {
layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;


lParamsDelete.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
lParamsDelete.width = WindowManager.LayoutParams.WRAP_CONTENT;
lParamsDelete.height = WindowManager.LayoutParams.WRAP_CONTENT;

// touch floating
binding.floating.setOnTouchListener(
binding.textSpeed.setOnLongClickListener(
view -> {
stopSelf();
return true;
});

binding.back.setOnTouchListener(
new View.OnTouchListener() {
int x = 0;
int y = 0;
Expand Down Expand Up @@ -183,7 +213,8 @@ public boolean onTouch(View view, MotionEvent event) {
});
// view
windowManager.addView(binding.getRoot(), layoutParams);

// windowManager.addView(delete.getRoot(), lParamsDelete);

ConnectivityManager connectivityManager =
(ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
Expand All @@ -192,15 +223,15 @@ public boolean onTouch(View view, MotionEvent event) {
connectivityManager.getActiveNetwork());
if (network != null) {
if (network.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
binding.connection.setImageResource(R.drawable.ic_data_lte_20px);
binding.connection.setImageResource(R.drawable.ic_signal_mobile);
} else if (network.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
binding.connection.setImageResource(R.drawable.ic_nauta_fill_24px);
binding.connection.setImageResource(R.drawable.ic_signal_wifi);
} else {
binding.connection.setImageResource(R.drawable.ic_calendar_20px);
binding.connection.setImageResource(R.drawable.ic_signal_none);
}
}
}

// crear notofocacion
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel =
Expand All @@ -217,8 +248,12 @@ public boolean onTouch(View view, MotionEvent event) {
getString(R.string.app_name)
+ " se está ejecutando en segundo plano")
.setSmallIcon(R.drawable.ic_logo_simple);
int NOTIFICATION_ID = 1;
startForeground(NOTIFICATION_ID, builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
1, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
} else {
startForeground(1, builder.build());
}
handler.post(runnable);
}

Expand All @@ -229,27 +264,44 @@ public void onDestroy() {
windowManager.removeView(binding.getRoot());
}
}
private boolean connected(){
ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info != null && info.isConnected()){
return true;
}else{
return false;
}
}

private void startFloating(){
if(permissions()){


private boolean connected() {
ConnectivityManager manager =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
return true;
} else {
return false;
}
}

private boolean permissions(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
return Settings.canDrawOverlays(this);


private void startFloating() {
if (permissions()) {}
}

private boolean permissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(this);
}
return true;
}

private boolean estaEncimaDelCesto(int x, int y) {
// Calcula la posición del cesto en la pantalla (ajusta según tus necesidades)
int[] cestoLocation = new int[2];
delete.getRoot().getLocationOnScreen(cestoLocation);
int cestoX = cestoLocation[0];
int cestoY = cestoLocation[1];

// Comprueba si las coordenadas están dentro del área del cesto
if (x >= cestoX
&& x <= cestoX + delete.getRoot().getWidth()
&& y >= cestoY
&& y <= cestoY + delete.getRoot().getHeight()) {
return true;
}

return false;
}
}
Loading

0 comments on commit 4971b9b

Please sign in to comment.