Last active
February 13, 2017 11:28
-
-
Save PepDevils/ce5d5d32d6240bd550c5d02e23daaaa2 to your computer and use it in GitHub Desktop.
Actividade que vai iniciar o serviço do botão com a runtime autorization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final static int REQUEST_CODE = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_initial); | |
checkDrawOverlayPermission(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
*//** check if received result code | |
is equal our requested code for draw permission *//* | |
if (requestCode == REQUEST_CODE) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (Settings.canDrawOverlays(this)) { | |
// continue here - permission was granted | |
} | |
} | |
} | |
} | |
public void checkDrawOverlayPermission() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (!Settings.canDrawOverlays(this)) { | |
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, | |
Uri.parse("package:" + getPackageName())); | |
startActivityForResult(intent, REQUEST_CODE); | |
} | |
} | |
} | |
//MANIFEST | |
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> | |
//SERVICE | |
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.PixelFormat; | |
import android.os.Build; | |
import android.os.IBinder; | |
import android.support.annotation.Nullable; | |
import android.util.DisplayMetrics; | |
import android.view.Gravity; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import android.view.animation.LinearInterpolator; | |
import android.widget.LinearLayout; | |
public class FloatingButtonService extends Service { | |
public static WindowManager wm; | |
public static LinearLayout ll; | |
public static ViewGroup mParentView; // for possible animate | |
//private Button Stop; | |
private int Original_mdpi_size_button = 80; //48 | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
wm = (WindowManager) getSystemService(WINDOW_SERVICE); | |
DisplayMetrics metrics = new DisplayMetrics(); | |
wm.getDefaultDisplay().getMetrics(metrics); | |
float screenDensityScale = metrics.scaledDensity; | |
int height = (int) (metrics.heightPixels / screenDensityScale); | |
int width = (int) (metrics.widthPixels / screenDensityScale); | |
ll = new LinearLayout(this); | |
/* Stop = new Button(this); | |
WindowManager.LayoutParams btParametrs = new WindowManager.LayoutParams((int) (Original_mdpi_size_button * screenDensityScale), | |
(int) (Original_mdpi_size_button * screenDensityScale), | |
WindowManager.LayoutParams.TYPE_PHONE, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); | |
Stop.setText("Fechar"); | |
Stop.setLayoutParams(btParametrs);*/ | |
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); | |
// ll.setBackgroundColor(Color.argb(66, 255, 0, 0)); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
ll.setBackground(getResources().getDrawable(R.mipmap.ic_launcher, getTheme())); | |
} else { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
ll.setBackground(getResources().getDrawable(R.mipmap.ic_launcher)); | |
} | |
} | |
ll.setLayoutParams(llp); | |
WindowManager.LayoutParams parameters = new WindowManager.LayoutParams((int) (Original_mdpi_size_button * screenDensityScale), | |
(int) (Original_mdpi_size_button * screenDensityScale), | |
WindowManager.LayoutParams.TYPE_PHONE, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, | |
PixelFormat.TRANSLUCENT); | |
parameters.gravity = Gravity.CENTER; | |
parameters.y = 0; | |
parameters.x = 0; | |
/* final WindowManager.LayoutParams params_mParentView = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, | |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY *//*| WindowManager.LayoutParams.TYPE_PHONE*//*, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | | |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, | |
PixelFormat.TRANSLUCENT); | |
params_mParentView.gravity = Gravity.CENTER; | |
params_mParentView.y = 0; | |
params_mParentView.x = 0; | |
mParentView = new FrameLayout(this); | |
wm.addView(mParentView, params_mParentView); | |
mParentView.addView(ll, parameters);*/ | |
//wm.addView(ll, params_mParentView); | |
wm.addView(ll, parameters); | |
ll.setVisibility(VISIBLE); | |
// wm.addView(Stop, btParametrs); | |
//wm.addView(ll, parameters); | |
/* ll.setOnTouchListener(new View.OnTouchListener() { | |
private WindowManager.LayoutParams updatedParameters = parameters; | |
private int x, y; | |
private float touchedX, touchedY; | |
@Override | |
public boolean onTouch(View view, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
x = updatedParameters.x; | |
y = updatedParameters.y; | |
touchedX = event.getRawX(); | |
touchedY = event.getRawY(); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
updatedParameters.x = (int) (x + (event.getRawX() - touchedX)); | |
updatedParameters.y = (int) (y + (event.getRawY() - touchedY)); | |
wm.updateViewLayout(ll, updatedParameters); | |
break; | |
default: | |
break; | |
} | |
return false; | |
} | |
});*/ | |
ll.setOnLongClickListener(view -> { | |
// mParentView.setOnClickListener(view -> { | |
dialogIntent = new Intent(FloatingButtonService.this, TVActivity.class); | |
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); // esta flag cria uma nova actividade sempre que esta é chamada | |
startActivity(dialogIntent); | |
return false; | |
}); | |
/* Stop.setOnClickListener(view -> { | |
wm.removeView(ll); | |
wm.removeView(Stop); | |
stopSelf(); | |
});*/ | |
//StartButtonAnimation(); | |
} | |
public static Intent dialogIntent; | |
private void StartButtonAnimation() { | |
// http://www.androidhive.info/2013/06/android-working-with-xml-animations/ | |
ll.setVisibility(VISIBLE); | |
final Animation in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.loop_translation); | |
in.setRepeatCount(Animation.INFINITE); | |
in.setRepeatMode(Animation.RESTART); | |
in.setFillAfter(true); | |
in.setInterpolator(new LinearInterpolator()); | |
//in.setInterpolator(new AccelerateInterpolator()); | |
//in.setInterpolator(new CycleInterpolator(2f)); | |
//in.setInterpolator((new AccelerateDecelerateInterpolator())); | |
in.setDuration(16000); | |
in.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
in.setInterpolator(new LinearInterpolator()); | |
ll.startAnimation(in); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
ll.startAnimation(in); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment