• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.ddm;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 import android.util.Log;
22 
23 import org.apache.harmony.dalvik.ddmc.Chunk;
24 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
25 import org.apache.harmony.dalvik.ddmc.DdmServer;
26 
27 import java.nio.ByteBuffer;
28 
29 
30 /**
31  * Track our app name.  We don't (currently) handle any inbound packets.
32  */
33 public class DdmHandleAppName extends DdmHandle {
34 
35     public static final int CHUNK_APNM = ChunkHandler.type("APNM");
36 
37     private static volatile Names sNames = new Names("", "");
38 
39     private static DdmHandleAppName mInstance = new DdmHandleAppName();
40 
41 
42     /* singleton, do not instantiate */
DdmHandleAppName()43     private DdmHandleAppName() {}
44 
45     /**
46      * Register for the messages we're interested in.
47      */
register()48     public static void register() {}
49 
50     /**
51      * Called when the DDM server connects.  The handler is allowed to
52      * send messages to the server.
53      */
onConnected()54     public void onConnected() {}
55 
56     /**
57      * Called when the DDM server disconnects.  Can be used to disable
58      * periodic transmissions or clean up saved state.
59      */
onDisconnected()60     public void onDisconnected() {}
61 
62     /**
63      * Handle a chunk of data.
64      */
handleChunk(Chunk request)65     public Chunk handleChunk(Chunk request) {
66         return null;
67     }
68 
69 
70 
71     /**
72      * Sets all names to the same name.
73      */
74     @UnsupportedAppUsage
setAppName(String name, int userId)75     public static void setAppName(String name, int userId) {
76         setAppName(name, name, userId);
77     }
78 
79     /**
80      * Set the application name.  Called when we get named, which may be
81      * before or after DDMS connects.  For the latter we need to send up
82      * an APNM message.
83      */
84     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
setAppName(String appName, String pkgName, int userId)85     public static void setAppName(String appName, String pkgName, int userId) {
86         if (appName == null || appName.isEmpty() || pkgName == null || pkgName.isEmpty()) return;
87 
88         sNames = new Names(appName, pkgName);
89 
90         // if DDMS is already connected, send the app name up
91         sendAPNM(appName, pkgName, userId);
92     }
93 
94     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getNames()95     public static Names getNames() {
96         return sNames;
97     }
98 
99     /**
100      * Send an APNM (APplication NaMe) chunk.
101      */
sendAPNM(String appName, String pkgName, int userId)102     private static void sendAPNM(String appName, String pkgName, int userId) {
103         if (false)
104             Log.v("ddm", "Sending app name");
105 
106         ByteBuffer out = ByteBuffer.allocate(
107                             4 /* appName's length */
108                             + appName.length() * 2 /* appName */
109                             + 4 /* userId */
110                             + 4 /* pkgName's length */
111                             + pkgName.length() * 2 /* pkgName */);
112         out.order(ChunkHandler.CHUNK_ORDER);
113         out.putInt(appName.length());
114         putString(out, appName);
115         out.putInt(userId);
116         out.putInt(pkgName.length());
117         putString(out, pkgName);
118 
119         Chunk chunk = new Chunk(CHUNK_APNM, out);
120         DdmServer.sendChunk(chunk);
121     }
122 
123     /**
124      * A class that encapsulates the app and package names into a single
125      * instance, effectively synchronizing the two names.
126      */
127     static final class Names {
128 
129         private final String mAppName;
130 
131         private final String mPkgName;
132 
Names(String appName, String pkgName)133         private Names(String appName, String pkgName) {
134             mAppName = appName;
135             mPkgName = pkgName;
136         }
137 
getAppName()138         public String getAppName() {
139             return mAppName;
140         }
141 
getPkgName()142         public String getPkgName() {
143             return mPkgName;
144         }
145 
146     }
147 
148 }
149 
150