Posts

Some thoughts about AppOpsManager and 4.4

Image
Android 4.3 introduced a new hidden feature: AppOps. Android 4.4 improved this feature, but it is still hidden. Someone talks about it as an "App Permission Manager". We have very few information about it, but it is a lot more than a simple permission manager. Currently there is no action to launch it. You can see it in Manifest.xml: https://github.com/android/platform_packages_apps_settings/blob/master/AndroidManifest.xml#L802 A simple way could be this: Intent intent = new Intent("android.settings.SETTINGS"); intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.applications.AppOpsSummary"); startActivity(intent); Here you can find info about apps which use location-permissions, contact-permissions, message-permissions.... In the list, for each app you can see the last use of this permission, and if you click on an item, you can see a full detail. Pay attention! You can disable permi...

NotificationListenerService and kitkat

Image
Android 4.3 (API 18) introduced NotificationListenerService . I published a post about it a few months ago. Android 4.4 (API 19) added new features, and now we can have a lot of extra info about a notification (before we need to use reflection to read some info). As in 4.3,to use the NotificationListenerService we have to extend the NotificationListenerService and implement onNotificationPosted() and onNotificationRemoved() methods public class SimpleKitkatNotificationListener extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { //.............. } @Override public void onNotificationRemoved(StatusBarNotification sbn) { //.............. } } Then we must declare the service in the manifest file with the BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action ...

Common tips about Gradle - part 2

In my previous post I wrote some common gradle tips. In this post I will continue to collect other common (and basic) cases. I would like to use the Eclipse folders structure in Android Studio. How to achieve that? In your build.gradle you can set this script: android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } } Where should I create the libs folder for my jars file? You can use this structure: - root - project libs myjar.jar src main java res build.gradle In build.gradle: dependencies { compile files('libs/myjar.jar') } Where should I create the aidl ...

Common tips about Gradle

Image
If you are using Gradle , you already know many of these tips. In the last days many devs (they are using Eclipse) asked me some questions about Gradle. I will not talk about what is Gradle, because you can find very good tutorial about it. I will collect some common (and basic) cases. First of all, if you are looking for a good tutorial, I suggest you these links: Official Guide Mark Allison's series Android Studio IDE helps you to manage Gradle files, but I suggest you understand what are the gradle files, so you can easily edit them manually. I've just created a new project It is important to understand the gradle structure. - root - myProject build.gradle setting.gradle build.gradle At the root project you will find 2 files: settings.gradle build.gradle settings.gradle tells Gradle at build time that there are sub-projects to build. Here an example: include ':myPproject' build.gradle defines how to build the project/module. ...

How to export a View as Bitmap

Image
In last days in my Card library , I tried to export a View as a Bitmap. A simple way to export the view is to use following code: Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); Pay attention where you will use this code . If you use this in onCreate() method, the View didn't go through a layout, so its size is null (0x0). In these cases you have to use something like this before drawing the bitmap: view.measure(widthSpec, heightSpec); view.layout(left, top, right, bottom); An example: int spec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED); view.measure(spec,spec); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canva...

Publish an aar file to Maven Central with Gradle

Image
In the last days I tried to publish ChangeLog Lib as aar (Android Archive) to Maven Central . First of all, I am a newbie in Gradle and Maven.I always used repo as a final user, so there could be better solutions. My first step was to find an approved repository . I decided to use the Sonatype OSS Repository , which is an approved repository provided by Sonatype. 1. Register your project at Sonatype It is easy. You can use the official guide . You have to follow the 2nd and 3th section. In this way you can create your user, and your project. When you create your project, pay attention to groupId . It is very important. Here you can find a some tips about it. If you don't own a domain, you can choose a groupId that reflects your project hosting, something like com.github.gabrielemariotti. You can't use it immediately; Sonatype have to prepare repo and normally it takes 1-2 business days. Then you can write your gradle scripts to upload aar file. 2. Grad...

A Card Library

Image
In the last weeks I tried to collect my code and build a Card library. I wanted something that could realize many types of Card (not all, of course), with some built-in features without having to rewrite each time a lot of code. I am still developing but a first version is presentable Card Library provides a custom tag CardView to display a UI Card. It provides different parts as a Header, a Thumbnail, a Shadow, a MainContentArea where you can inflate your custom layout You can customize the global layout as you like You can have some built-in features as OnClickListener, OnSwipeListener , OnLongClickListener CardHeader` provides an overflow button with a PopupMenuListener, a button to expand/collapse an area, or a customizable button with its listener. CardThumbnail` load a Bitmap with a resource ID or with a URL using `LRUCache` and an `AsyncTask` Create a `Card` is is pretty simple. First, you need an XML layout that will display the `Card`. Th...