Skip to content

Instantly share code, notes, and snippets.

Lorenzo Quiroli quiro91

Block or report user

Report or block quiro91

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Check App Usage Permission.java
private boolean checkForPermission(Context context) {
AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(OPSTR_GET_USAGE_STATS, Process.myUid(), context.getPackageName());
return mode == MODE_ALLOWED;
}
View Getting UsageStats.java
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
long start = calendar.getTimeInMillis();
long end = System.currentTimeMillis();
List<UsageStats> stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_WEEKLY, start, end);
View Getting a map of UsageStats.java
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
long start = calendar.getTimeInMillis();
long end = System.currentTimeMillis();
Map<String, UsageStats> stats = usageStatsManager.queryAndAggregateUsageStats(start, end);
@quiro91
quiro91 / measureWidth.java
Last active Jul 4, 2019
Measure minimum width
View measureWidth.java
private int measureDimension(int desiredSize, int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = desiredSize;
if (specMode == MeasureSpec.AT_MOST) {
View onMeasure.java
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.v("Chart onMeasure w", MeasureSpec.toString(widthMeasureSpec));
Log.v("Chart onMeasure h", MeasureSpec.toString(heightMeasureSpec));
int desiredWidth = getSuggestedMinimumWidth() + getPaddingLeft() + getPaddingRight();
int desiredHeight = getSuggestedMinimumHeight() + getPaddingTop() + getPaddingBottom();
setMeasuredDimension(measureDimension(desiredWidth, widthMeasureSpec),
measureDimension(desiredHeight, heightMeasureSpec));
View TakePicture.java
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFileWith();
} catch (IOException ex) {
Log.e("TakePicture", ex.getMessage());
}
if (photoFile != null) {
View TakePictureWithFileProvider.java
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Uri photoURI = null;
try {
File photoFile = createImageFileWith();
path = photoFile.getAbsolutePath();
photoURI = FileProvider.getUriForFile(MainActivity.this,
getString(R.string.file_provider_authority),
photoFile);
View TakePictureWithFileProvider2.java
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Uri photoURI = null;
try {
File photoFile = createImageFileWith();
path = photoFile.getAbsolutePath();
photoURI = FileProvider.getUriForFile(MainActivity.this,
getString(R.string.file_provider_authority),
photoFile);
View BindExt.kt
import android.app.Activity
import android.support.annotation.IdRes
import android.view.View
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
View Nullable cast
var something: String? = getSomething()
fun main(args: Array<String>) {
nowItsNotAVarAnymore(something)
}
fun nowItsNotAVarAnymore(param: String?) {
if (param) {
println("Param is not nullable and contains ${param.length} characters)
} else {
You can’t perform that action at this time.