Skip to content

Commit 8617edc

Browse files
author
tangyinsheng
committed
[tinker] Update getProcessName() implementation.
1 parent 1326660 commit 8617edc

File tree

2 files changed

+100
-110
lines changed

2 files changed

+100
-110
lines changed

tinker-android/tinker-android-loader-no-op/src/main/java/com/tencent/tinker/loader/shareutil/ShareTinkerInternals.java

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package com.tencent.tinker.loader.shareutil;
1818

19+
import android.annotation.SuppressLint;
1920
import android.app.ActivityManager;
21+
import android.app.ActivityManager.RunningAppProcessInfo;
22+
import android.app.Application;
2023
import android.content.Context;
2124
import android.content.SharedPreferences;
2225
import android.content.pm.ApplicationInfo;
2326
import android.content.pm.PackageManager;
2427
import android.os.Build;
2528
import android.text.TextUtils;
2629

30+
import java.io.BufferedInputStream;
2731
import java.io.ByteArrayOutputStream;
2832
import java.io.File;
2933
import java.io.FileInputStream;
@@ -32,6 +36,7 @@
3236
import java.io.PrintStream;
3337
import java.lang.reflect.InvocationTargetException;
3438
import java.lang.reflect.Method;
39+
import java.nio.charset.StandardCharsets;
3540
import java.util.HashMap;
3641
import java.util.List;
3742
import java.util.Properties;
@@ -54,9 +59,9 @@ public class ShareTinkerInternals {
5459
/**
5560
* or you may just hardcode them in your app
5661
*/
57-
private static String processName = null;
58-
private static String tinkerID = null;
59-
private static String currentInstructionSet = null;
62+
private static final String[] processName = {null};
63+
private static String tinkerID = null;
64+
private static String currentInstructionSet = null;
6065

6166
public static boolean isVmArt() {
6267
return VM_IS_ART || Build.VERSION.SDK_INT >= 21;
@@ -466,77 +471,71 @@ public static void killProcessExceptMain(Context context) {
466471
* @param context
467472
* @return
468473
*/
469-
public static String getProcessName(final Context context) {
470-
if (processName != null) {
471-
return processName;
474+
public static String getProcessName(Context context) {
475+
if (processName[0] == null) {
476+
synchronized (processName) {
477+
if (processName[0] == null) {
478+
processName[0] = getProcessNameInternal(context);
479+
}
480+
}
472481
}
473-
//will not null
474-
processName = getProcessNameInternal(context);
475-
return processName;
482+
return (processName[0] != null ? processName[0] : "");
476483
}
477484

478-
485+
@SuppressLint("NewApi")
479486
private static String getProcessNameInternal(final Context context) {
480-
int myPid = android.os.Process.myPid();
481-
482-
if (context == null || myPid <= 0) {
483-
return "";
487+
if (ShareTinkerInternals.isNewerOrEqualThanVersion(28, true)) {
488+
final String result = Application.getProcessName();
489+
if (!TextUtils.isEmpty(result)) {
490+
return result;
491+
}
484492
}
485493

486-
ActivityManager.RunningAppProcessInfo myProcess = null;
487-
ActivityManager activityManager =
488-
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
489-
490-
if (activityManager != null) {
494+
if (context != null) {
491495
try {
492-
List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager
493-
.getRunningAppProcesses();
494-
495-
if (appProcessList != null) {
496-
for (ActivityManager.RunningAppProcessInfo process : appProcessList) {
497-
if (process.pid == myPid) {
498-
myProcess = process;
499-
break;
496+
final int myPid = android.os.Process.myPid();
497+
final int myUid = android.os.Process.myUid();
498+
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
499+
if (am != null) {
500+
final List<RunningAppProcessInfo> procInfos = am.getRunningAppProcesses();
501+
if (procInfos != null) {
502+
for (RunningAppProcessInfo procInfo : procInfos) {
503+
if (procInfo.pid == myPid && procInfo.uid == myUid) {
504+
return procInfo.processName;
505+
}
500506
}
501507
}
502-
503-
if (myProcess != null) {
504-
return myProcess.processName;
505-
}
506508
}
507-
} catch (Exception e) {
508-
ShareTinkerLog.e(TAG, "getProcessNameInternal exception:" + e.getMessage());
509+
} catch (Throwable ignored) {
510+
// Ignored.
509511
}
510512
}
511513

512-
byte[] b = new byte[128];
513-
FileInputStream in = null;
514+
final byte[] buf = new byte[2048];
515+
InputStream in = null;
514516
try {
515-
in = new FileInputStream("/proc/" + myPid + "/cmdline");
516-
int len = in.read(b);
517+
in = new BufferedInputStream(new FileInputStream("/proc/self/cmdline"));
518+
int len = in.read(buf);
519+
while (len > 0 && (buf[len - 1] <= 0 || buf[len - 1] == 10 || buf[len - 1] == 13)) --len;
517520
if (len > 0) {
518-
for (int i = 0; i < len; i++) { // lots of '0' in tail , remove them
519-
if ((((int) b[i]) & 0xFF) > 128 || b[i] <= 0) {
520-
len = i;
521-
break;
522-
}
523-
}
524-
return new String(b, 0, len);
521+
return new String(buf, StandardCharsets.US_ASCII);
525522
}
526-
527-
} catch (Exception e) {
528-
ShareTinkerLog.e(TAG, "getProcessNameInternal exception:" + e.getMessage());
523+
} catch (Throwable thr) {
524+
ShareTinkerLog.e(TAG, "getProcessNameInternal parse cmdline exception:" + thr.getMessage());
529525
} finally {
530-
try {
531-
if (in != null) {
532-
in.close();
533-
}
534-
} catch (Exception e) {
535-
// Ignored.
536-
}
526+
SharePatchFileUtil.closeQuietly(in);
537527
}
538528

539-
return "";
529+
return null;
530+
}
531+
532+
private static boolean isNewerOrEqualThanVersion(int apiLevel, boolean includePreviewVer) {
533+
if (includePreviewVer && Build.VERSION.SDK_INT >= 23) {
534+
return Build.VERSION.SDK_INT >= apiLevel
535+
|| ((Build.VERSION.SDK_INT == apiLevel - 1) && Build.VERSION.PREVIEW_SDK_INT > 0);
536+
} else {
537+
return Build.VERSION.SDK_INT >= apiLevel;
538+
}
540539
}
541540

542541
/**

tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/shareutil/ShareTinkerInternals.java

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
package com.tencent.tinker.loader.shareutil;
1818

19+
import android.annotation.SuppressLint;
1920
import android.app.ActivityManager;
21+
import android.app.ActivityManager.RunningAppProcessInfo;
22+
import android.app.Application;
2023
import android.content.Context;
2124
import android.content.SharedPreferences;
2225
import android.content.pm.ApplicationInfo;
2326
import android.content.pm.PackageManager;
2427
import android.os.Build;
28+
import android.text.TextUtils;
2529

2630
import com.tencent.tinker.loader.TinkerRuntimeException;
2731

32+
import java.io.BufferedInputStream;
2833
import java.io.ByteArrayOutputStream;
2934
import java.io.DataInputStream;
3035
import java.io.DataOutputStream;
@@ -36,6 +41,7 @@
3641
import java.io.PrintStream;
3742
import java.lang.reflect.InvocationTargetException;
3843
import java.lang.reflect.Method;
44+
import java.nio.charset.StandardCharsets;
3945
import java.util.HashMap;
4046
import java.util.List;
4147
import java.util.Properties;
@@ -58,9 +64,9 @@ public class ShareTinkerInternals {
5864
/**
5965
* or you may just hardcode them in your app
6066
*/
61-
private static String processName = null;
62-
private static String tinkerID = null;
63-
private static String currentInstructionSet = null;
67+
private static final String[] processName = {null};
68+
private static String tinkerID = null;
69+
private static String currentInstructionSet = null;
6470

6571
public static boolean isVmArt() {
6672
return VM_IS_ART || Build.VERSION.SDK_INT >= 21;
@@ -531,77 +537,62 @@ public static void killProcessExceptMain(Context context) {
531537
* @param context
532538
* @return
533539
*/
534-
public static String getProcessName(final Context context) {
535-
if (processName != null) {
536-
return processName;
540+
public static String getProcessName(Context context) {
541+
if (processName[0] == null) {
542+
synchronized (processName) {
543+
if (processName[0] == null) {
544+
processName[0] = getProcessNameInternal(context);
545+
}
546+
}
537547
}
538-
//will not null
539-
processName = getProcessNameInternal(context);
540-
return processName;
548+
return (processName[0] != null ? processName[0] : "");
541549
}
542550

543-
551+
@SuppressLint("NewApi")
544552
private static String getProcessNameInternal(final Context context) {
545-
int myPid = android.os.Process.myPid();
546-
547-
if (context == null || myPid <= 0) {
548-
return "";
553+
if (ShareTinkerInternals.isNewerOrEqualThanVersion(28, true)) {
554+
final String result = Application.getProcessName();
555+
if (!TextUtils.isEmpty(result)) {
556+
return result;
557+
}
549558
}
550559

551-
ActivityManager.RunningAppProcessInfo myProcess = null;
552-
ActivityManager activityManager =
553-
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
554-
555-
if (activityManager != null) {
560+
if (context != null) {
556561
try {
557-
List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager
558-
.getRunningAppProcesses();
559-
560-
if (appProcessList != null) {
561-
for (ActivityManager.RunningAppProcessInfo process : appProcessList) {
562-
if (process.pid == myPid) {
563-
myProcess = process;
564-
break;
562+
final int myPid = android.os.Process.myPid();
563+
final int myUid = android.os.Process.myUid();
564+
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
565+
if (am != null) {
566+
final List<RunningAppProcessInfo> procInfos = am.getRunningAppProcesses();
567+
if (procInfos != null) {
568+
for (RunningAppProcessInfo procInfo : procInfos) {
569+
if (procInfo.pid == myPid && procInfo.uid == myUid) {
570+
return procInfo.processName;
571+
}
565572
}
566573
}
567-
568-
if (myProcess != null) {
569-
return myProcess.processName;
570-
}
571574
}
572-
} catch (Exception e) {
573-
ShareTinkerLog.e(TAG, "getProcessNameInternal exception:" + e.getMessage());
575+
} catch (Throwable ignored) {
576+
// Ignored.
574577
}
575578
}
576579

577-
byte[] b = new byte[128];
578-
FileInputStream in = null;
580+
final byte[] buf = new byte[2048];
581+
InputStream in = null;
579582
try {
580-
in = new FileInputStream("/proc/" + myPid + "/cmdline");
581-
int len = in.read(b);
583+
in = new BufferedInputStream(new FileInputStream("/proc/self/cmdline"));
584+
int len = in.read(buf);
585+
while (len > 0 && (buf[len - 1] <= 0 || buf[len - 1] == 10 || buf[len - 1] == 13)) --len;
582586
if (len > 0) {
583-
for (int i = 0; i < len; i++) { // lots of '0' in tail , remove them
584-
if ((((int) b[i]) & 0xFF) > 128 || b[i] <= 0) {
585-
len = i;
586-
break;
587-
}
588-
}
589-
return new String(b, 0, len);
587+
return new String(buf, StandardCharsets.US_ASCII);
590588
}
591-
592-
} catch (Exception e) {
593-
ShareTinkerLog.e(TAG, "getProcessNameInternal exception:" + e.getMessage());
589+
} catch (Throwable thr) {
590+
ShareTinkerLog.e(TAG, "getProcessNameInternal parse cmdline exception:" + thr.getMessage());
594591
} finally {
595-
try {
596-
if (in != null) {
597-
in.close();
598-
}
599-
} catch (Exception e) {
600-
// Ignored.
601-
}
592+
SharePatchFileUtil.closeQuietly(in);
602593
}
603594

604-
return "";
595+
return null;
605596
}
606597

607598
/**

0 commit comments

Comments
 (0)