Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
YeChao committed Oct 22, 2020
1 parent 50221af commit 9e29187
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
16 changes: 8 additions & 8 deletions yutils/src/main/java/com/yechaoa/yutils/YUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ public class YUtils {

private static Toast toast;
private static ProgressDialog progressDialog;
private static Application mApplicationContext;
private static Application mApp;

/**
* 使用init()即可
*/
@Deprecated
public static void initialize(Application app) {
mApplicationContext = app;
mApp = app;
app.registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
}


public static void init(Application app) {
mApplicationContext = app;
mApp = app;
app.registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
}

Expand All @@ -58,11 +58,11 @@ public static void init(Application app) {
*/
@Deprecated
public static Application getApplication() {
return mApplicationContext;
return mApp;
}

public static Application getApp() {
return mApplicationContext;
return mApp;
}

/**
Expand Down Expand Up @@ -146,8 +146,8 @@ public void run() {
*/
public static String getVersionName() {
try {
PackageManager packageManager = YUtils.getApplication().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApplication().getPackageName(), 0);
PackageManager packageManager = YUtils.getApp().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApp().getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Expand All @@ -161,7 +161,7 @@ public static String getVersionName() {
public static int getVersionCode() {
try {
PackageManager packageManager = YUtils.getApp().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApplication().getPackageName(), 0);
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApp().getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import android.content.SharedPreferences
*/
object SpUtil {
private const val FILE_NAME = "config"
private val sp: SharedPreferences = YUtils.getApp()!!.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
private val sp: SharedPreferences = YUtils.getApp().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)

/**
* String
Expand Down
38 changes: 19 additions & 19 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ import java.util.regex.Pattern
*/
object YUtils {

private var mApplicationContext: Application? = null
private lateinit var mApp: Application
private var yLoadingDialog: YLoadingDialog? = null

@Deprecated("简化调用,使用init(app)即可", ReplaceWith("YUtilsKt.init(app)"))
fun initialize(app: Application?) {
mApplicationContext = app
app!!.registerActivityLifecycleCallbacks(ActivityUtil.activityLifecycleCallbacks)
fun initialize(app: Application) {
mApp = app
app.registerActivityLifecycleCallbacks(ActivityUtil.activityLifecycleCallbacks)
}

fun init(app: Application?) {
mApplicationContext = app
app!!.registerActivityLifecycleCallbacks(ActivityUtil.activityLifecycleCallbacks)
fun init(app: Application) {
mApp = app
app.registerActivityLifecycleCallbacks(ActivityUtil.activityLifecycleCallbacks)
}

@Deprecated("简化调用,使用getApp()即可", ReplaceWith("YUtilsKt.getApp()"))
fun getApplication(): Application? {
return mApplicationContext
fun getApplication(): Application {
return mApp
}

fun getApp(): Application? {
return mApplicationContext
fun getApp(): Application {
return mApp
}

/**
Expand Down Expand Up @@ -117,8 +117,8 @@ object YUtils {
*/
fun getVersionName(): String? {
return try {
val packageManager = getApp()!!.packageManager
val packageInfo = packageManager.getPackageInfo(getApp()!!.packageName, 0)
val packageManager = getApp().packageManager
val packageInfo = packageManager.getPackageInfo(getApp().packageName, 0)
packageInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
Expand All @@ -131,8 +131,8 @@ object YUtils {
*/
fun getVersionCode(): Int {
return try {
val packageManager = getApp()!!.packageManager
val packageInfo = packageManager.getPackageInfo(getApp()!!.packageName, 0)
val packageManager = getApp().packageManager
val packageInfo = packageManager.getPackageInfo(getApp().packageName, 0)
packageInfo.versionCode
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
Expand Down Expand Up @@ -172,24 +172,24 @@ object YUtils {
* dp2px
*/
fun dp2px(dp: Float): Int {
val density = getApp()!!.resources.displayMetrics.density
val density = getApp().resources.displayMetrics.density
return (dp * density + 0.5f).toInt()
}

/**
* px2dp
*/
fun px2dp(px: Int): Float {
val density = getApp()!!.resources.displayMetrics.density
val density = getApp().resources.displayMetrics.density
return px / density
}

/**
* 复制文本到粘贴板
*/
fun copyToClipboard(text: String?) {
val cm = getApp()!!.getSystemService(Activity.CLIPBOARD_SERVICE) as ClipboardManager
cm.primaryClip = ClipData.newPlainText(getApp()!!.packageName, text)
val cm = getApp().getSystemService(Activity.CLIPBOARD_SERVICE) as ClipboardManager
cm.primaryClip = ClipData.newPlainText(getApp().packageName, text)
}

/**
Expand Down

0 comments on commit 9e29187

Please sign in to comment.