Skip to content

Instantly share code, notes, and snippets.

View quiro91's full-sized avatar

Lorenzo Quiroli quiro91

  • ClearScore
  • Barcelona
View GitHub Profile
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;
}
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);
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 July 4, 2019 06:28
Measure minimum width
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) {
@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));
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) {
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);
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);
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> {
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 {