Skip to content

Instantly share code, notes, and snippets.

@kikuchy
Last active September 29, 2017 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikuchy/287ff3f99710a3c7fd95 to your computer and use it in GitHub Desktop.
Save kikuchy/287ff3f99710a3c7fd95 to your computer and use it in GitHub Desktop.
Extension functions for ContextCompat methods
package net.kikuchy.compatkotlinextention
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
/*
Copyright 2016 kikuchy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getColor(android.content.Context,%20int)">getColor(Context, int)</a>
*/
public fun Context.getColorCompat(id: Int) = ContextCompat.getColor(this, id)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#checkSelfPermission(android.content.Context,%20java.lang.String)">checkSelfPermission (Context, String)</a>
*/
public fun Context.checkSelfPermissionCompat(permission: String) = ContextCompat.checkSelfPermission(this, permission)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getCodeCacheDir(android.content.Context)">getCodeCacheDir (Context)</a>
*/
public fun Context.getCodeCacheDirCompat() = ContextCompat.getCodeCacheDir(this)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getColorStateList(android.content.Context,%20int)">getColorStateList(Context, int)</a>
*/
public fun Context.getColorStateListCompat(id: Int) = ContextCompat.getColorStateList(this, id)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getDrawable(android.content.Context,%20int)">getDrawable(Context, int)</a>
*/
public fun Context.getDrawableCompat(id: Int) = ContextCompat.getDrawable(this, id)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalCacheDirs(android.content.Context)>getExternalCacheDirs(Context)</a>
*/
public fun Context.getExternalCacheDirsCompat() = ContextCompat.getExternalCacheDirs(this)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalFilesDirs(android.content.Context,%20java.lang.String)">getExternalFilesDirs(Context, String)</a>
*/
public fun Context.getExternalFilesDirsCompat(type: String) = ContextCompat.getExternalFilesDirs(this, type)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getNoBackupFilesDir(android.content.Context)">getNoBackupFilesDir(Context)</a>
*/
public fun Context.getNoBackupFilesDirCompat() = ContextCompat().getNoBackupFilesDir(this)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getObbDirs(android.content.Context)">getObbDirs(Context)</a>
*/
public fun Context.getObbDirsCompat() = ContextCompat.getObbDirs(this)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#startActivities(android.content.Context,%20android.content.Intent[])">startActivities(Context, Intent[])</a>
*/
public fun Context.startActivitiesCompat(intents: Array<Intent>) = ContextCompat.startActivities(this, intents)
/**
* @see <a href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#startActivities(android.content.Context,%20android.content.Intent[],%20android.os.Bundle)>startActivities(Context, Intent[], Bundle)</a>
*/
public fun Context.startActivitiesCompat(intents: Array<Intent>, options: Bundle) = ContextCompat.startActivities(this, intents, options)
@VincentMasselis
Copy link

VincentMasselis commented Sep 29, 2017

Unauthorized initialization for ContextCompat at the line 62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment