This is reprint, the original text see: http://www.cnblogs.com/coding-way/p/4294225.html
Recently looking at a foreigner wrote something, found inside there is a class, use this class can let any device use Hierarchyview.
As we all know, the market sells Android devices, generally can not use Hierarchyview, so take this opportunity to understand the hierarchyview principle of implementation, and learn the solution of foreigners.
Hierarchyview source in the/sdk/eclipse/plugins/com.android.ide.eclipse.hierarchyviewer, but seemingly not all,
So directly decompile/prebuilts/devtools/tools/lib/hierarchyviewer2lib.jar and/prebuilts/devtools/tools/lib/ Hierarchyviewer2.jar.
When using Hierarchyview on the device, Hierarchyview will send a startviewserver command to the device, the following source when the order of its call:
Hierarchyviewerdirector.class
public void Populatedeviceselectionmodel () {idevice[] devices = devicebridge.getdevices (); for (IDevice device:devices) deviceconnected (device); } public void deviceconnected (final IDevice device) {executeinbackground ("Connecting device", new Object () { public void Run () {if (!device.isonline ()) return; Ihvdevice Hvdevice; Synchronized (hierarchyviewerdirector.mdeviceslock) {Hvdevice = (ihvdevice) HierarchyViewerDirector.this.mDevices . get (device); if (Hvdevice = = null) {Hvdevice = Hvdevicefactory.create (device); Hvdevice.initializeviewdebug (); Hvdevice.addwindowchangelistener (Hierarchyviewerdirector.getdirector ()); HierarchyViewerDirector.this.mDevices.put (device, Hvdevice); } else {hvdevice.initializeviewdebug (); }} Deviceselectionmodel.getmodel (). AddDevice (Hvdevice); HierarchyViewerDirector.this.focusChanged (device); } }); }
Viewserverdevice.class
public boolean initializeviewdebug () { if (!this.mdevice.isonline ()) { return false; } Devicebridge.setupdeviceforward (this.mdevice); return Reloadwindows (); } public boolean reloadwindows () { if (! Devicebridge.isviewserverrunning (This.mdevice)) && (! Devicebridge.startviewserver (This.mdevice)) { log.e ("Viewserverdevice", "Unable to debug device:" + This.mDevice.getName ()); Devicebridge.removedeviceforward (this.mdevice); return false; } This.mviewserverinfo = Devicebridge.loadviewserverinfo (this.mdevice); if (This.mviewserverinfo = = null) { return false; } This.mwindows = Devicebridge.loadwindows (this, this.mdevice); return true; }
Devicebridge.class
public static Boolean startviewserver (IDevice device) {return startviewserver (device, 4939); } public static Boolean startviewserver (IDevice device, int port) {boolean[] result = new BOOLEAN[1]; try {if (Device.isonline ()) Device.executeshellcommand (Buildstartservershellcommand (port), new booleanresultr Eader (result)); } catch (TimeoutException e) {log.e ("Hierarchyviewer", "Timeout starting view server on device" + device); } catch (IOException e) {log.e ("Hierarchyviewer", "Unable to start view server" + device); } catch (Adbcommandrejectedexception e) {log.e ("Hierarchyviewer", "Adb rejected command to start view server on Devi CE "+ device); } catch (Shellcommandunresponsiveexception e) {log.e ("Hierarchyviewer", "unable to execute command to start view Ser Ver on device + device); } return result[0]; } private static String buildstartservershellcommand (int port) {return String.Format ("service Call window%d i32%d ", new object[] {integer.valueof (1), integer.valueof (Port)}); }
As you can see from the code, the final hierarchyview will let the device execute the service command, the final command being executed:
[Email protected]:/$ Service Call window 1 i32 4939
This line of command actually sends a parcel with code 1 and a value of 4939 to Android.view.IWindowManager.
In fact, call Windowmanagerservice in the Startviewserver method, and the 4939 as a parameter, then see Windowmanagerservice.startviewserver Source:
public boolean startviewserver (int port) {if (Issystemsecure ()) {return false; } if (!checkcallingpermission (Manifest.permission.DUMP, "Startviewserver")) {return false; } if (Port < 1024x768) {return false; if (mviewserver! = null) {if (!mviewserver.isrunning ()) {try {ret Urn Mviewserver.start (); } catch (IOException e) {SLOG.W (TAG, "View server did not start"); }} return false; } try {mviewserver = new Viewserver (this, port); return Mviewserver.start (); } catch (IOException e) {SLOG.W (TAG, "View server did not start"); } return false; } private Boolean issystemsecure () {return ' 1 '. Equals (Systemproperties.get (System_secure, "1")) && "0". Equals (Systemproperties.get (system_dEbuggable, "0")); }
Inside will do some permission check, and then call Viewserver.start (), the key is in the Viewserver, first bar viewserver complete code paste:
Viewserver.java
As you can see, Viewserver implements Runnable and then looks at the implementation of start:
Boolean start () throws IOException {if (mthread! = null) {return false; } mserver = new ServerSocket (mport, View_server_max_connections, Inetaddress.getlocalhost ()); Mthread = new Thread (this, "Remote View Server [port=" + mport + "]"); Mthreadpool = Executors.newfixedthreadpool (view_server_max_connections); Mthread.start (); return true; public void Run () {while (thread.currentthread () = = Mthread) {//any uncaught exception would cras H The system process try {Socket client = mserver.accept (); if (Mthreadpool! = null) {Mthreadpool.submit (new Viewserverworker (client)); } else {try {client.close (); } catch (IOException e) {e.printstacktrace (); }}} catch (Exception e) {SLOG.W(Log_tag, "Connection error:", e); } } }
After this server is started, use the port number (4939) that was passed in before to create a serversocket, and then in a separate thread to listen to the port has a client connection request, and some words to Viewserverworker to handle:
Class Viewserverworker implements Runnable, Windowmanagerservice.windowchangelistener {private Socket mclient; Private Boolean mneedwindowlistupdate; Private Boolean mneedfocusedwindowupdate; Public Viewserverworker (Socket client) {mclient = client; Mneedwindowlistupdate = false; Mneedfocusedwindowupdate = false; public void Run () {bufferedreader in = null; try {in = new BufferedReader (New InputStreamReader (Mclient.getinputstream ()), 1024); Final String request = In.readline (); String command; String parameters; int index = Request.indexof ("); if (index = =-1) {command = Request; Parameters = ""; } else {command = request.substring (0, index); Parameters = request.substring (index + 1); } Boolean result; if (Command_protocol_version.equalsignorecase (COMMAND)) {result = WriteValue (mclient, Value_protocol_v Ersion); } else if (Command_server_version.equalsignorecase (COMMAND)) {result = WriteValue (mclient, Value_serve R_version); } else if (Command_window_manager_list.equalsignorecase (COMMAND)) {result = Mwindowmanager.viewserverl Istwindows (mclient); } else if (Command_window_manager_get_focus.equalsignorecase (COMMAND)) {result = Mwindowmanager.viewse Rvergetfocusedwindow (mclient); } else if (Command_window_manager_autolist.equalsignorecase (COMMAND)) {result = Windowmanagerautolistl OOP (); } else {result = Mwindowmanager.viewserverwindowcommand (mclient, command, p Arameters); } if (!result) { SLOG.W (Log_tag, "An error occurred with the command:" + command); }} catch (IOException e) {SLOG.W (Log_tag, "Connection error:", e); } finally {if (in! = null) {try {in.close (); } catch (IOException e) {e.printstacktrace (); }} if (mclient! = null) {try {mclient.close (); } catch (IOException e) {e.printstacktrace (); }}}} public void Windowschanged () {synchronized (this) { Mneedwindowlistupdate = true; Notifyall (); }} public void Focuschanged () {synchronized (this) {mneedfocusedwindowupdate = t Rue Notifyall (); }} private Boolean Windowmanagerautolistloop () {Mwindowmanager.addwindowchangelistener (this); BufferedWriter out = null; try {out = new BufferedWriter (New OutputStreamWriter (Mclient.getoutputstream ())); while (! Thread.interrupted ()) {Boolean needwindowlistupdate = false; Boolean needfocusedwindowupdate = false; Synchronized (this) {while (!mneedwindowlistupdate &&!mneedfocusedwindowupdate) { Wait (); } if (mneedwindowlistupdate) {mneedwindowlistupdate = false; Needwindowlistupdate = true; } if (mneedfocusedwindowupdate) {mneedfocusedwindowupdate = false; Needfocusedwindowupdate = true; }} if (Needwindowlistupdate) {out.write ("L IST update\n "); Out.flush (); } if (needfocusedwindowupdate) {out.write ("Action_focus update\n"); Out.flush (); }}} catch (Exception e) {//Ignore} finally {if (O UT! = null) {try {out.close (); } catch (IOException e) {//Ignore}} Mwindowman Ager.removewindowchangelistener (this); } return true; } }
As you can see from the code, Hierarchyview sends commands to the device through the socket, Viewserverworker to parse the processing command, and sends the value returned by the socket to Hierarchyview.
So far, the general principle of Hierarchyview has been understood, found that as long as we create a serversocket ourselves, and listen to 4939 ports, and then imitate viewserver processing the corresponding command can be used hierarchyview the device.
This is the method that foreigners use. So we don't have to reinvent the wheel.
Next look at the foreigner's solution:
problem-Solving classes
Here's how to use it:
public class MyActivity extends Activity {public void OnCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); Set content view, etc. Viewserver.get (This). AddWindow (this); } public void OnDestroy () { Super.ondestroy (); Viewserver.get (This). Removewindow (this); } public void Onresume () { super.onresume (); Viewserver.get (This). Setfocusedwindow (this); } }
Be careful when using the app to add Internet permissions, and android:debugable to True,eclipse or studio run directly to the phone is debugable, so don't worry.
Well, I wish you a happy Spring festival!
Hierarchyview implementation principle and Android device cannot use Hierarchyview workaround