1616
1717package com .tencent .tinker .loader .shareutil ;
1818
19+ import android .annotation .SuppressLint ;
1920import android .app .ActivityManager ;
21+ import android .app .ActivityManager .RunningAppProcessInfo ;
22+ import android .app .Application ;
2023import android .content .Context ;
2124import android .content .SharedPreferences ;
2225import android .content .pm .ApplicationInfo ;
2326import android .content .pm .PackageManager ;
2427import android .os .Build ;
2528import android .text .TextUtils ;
2629
30+ import java .io .BufferedInputStream ;
2731import java .io .ByteArrayOutputStream ;
2832import java .io .File ;
2933import java .io .FileInputStream ;
3236import java .io .PrintStream ;
3337import java .lang .reflect .InvocationTargetException ;
3438import java .lang .reflect .Method ;
39+ import java .nio .charset .StandardCharsets ;
3540import java .util .HashMap ;
3641import java .util .List ;
3742import 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 /**
0 commit comments