public class

ViewServer

extends Object
implements Runnable
java.lang.Object
   ↳ com.c2call.sdk.pub.util.ViewServer

Class Overview

This class can be used to enable the use of HierarchyViewer inside an application. HierarchyViewer is an Android SDK tool that can be used to inspect and debug the user interface of running applications. For security reasons, HierarchyViewer does not work on production builds (for instance phones bought in store.) By using this class, you can make HierarchyViewer work on any device. You must be very careful however to only enable HierarchyViewer when debugging your application.

To use this view server, your application must require the INTERNET permission.

The recommended way to use this API is to register activities when they are created, and to unregister them when they get destroyed:

 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);
     }
 }
 

In a similar fashion, you can use this API with an InputMethodService:

 public class MyInputMethodService extends InputMethodService {
     public void onCreate() {
         super.onCreate();
         View decorView = getWindow().getWindow().getDecorView();
         String name = "MyInputMethodService";
         ViewServer.get(this).addWindow(decorView, name);
     }

     public void onDestroy() {
         super.onDestroy();
         View decorView = getWindow().getWindow().getDecorView();
         ViewServer.get(this).removeWindow(decorView);
     }

     public void onStartInput(EditorInfo attribute, boolean restarting) {
         super.onStartInput(attribute, restarting);
         View decorView = getWindow().getWindow().getDecorView();
         ViewServer.get(this).setFocusedWindow(decorView);
     }
 }
 

Summary

Public Methods
void addWindow(Activity activity)
Invoke this method to register a new view hierarchy.
void addWindow(View view, String name)
Invoke this method to register a new view hierarchy.
static ViewServer get(Context context)
Returns a unique instance of the ViewServer.
boolean isRunning()
Indicates whether the server is currently running.
void removeWindow(Activity activity)
Invoke this method to unregister a view hierarchy.
void removeWindow(View view)
Invoke this method to unregister a view hierarchy.
void run()
Main server loop.
void setFocusedWindow(View view)
Invoke this method to change the currently focused window.
void setFocusedWindow(Activity activity)
Invoke this method to change the currently focused window.
boolean start()
Starts the server.
boolean stop()
Stops the server.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Runnable

Public Methods

public void addWindow (Activity activity)

Invoke this method to register a new view hierarchy.

Parameters
activity The activity whose view hierarchy/window to register

public void addWindow (View view, String name)

Invoke this method to register a new view hierarchy.

Parameters
view A view that belongs to the view hierarchy/window to register

public static ViewServer get (Context context)

Returns a unique instance of the ViewServer. This method should only be called from the main thread of your application. The server will have the same lifetime as your process. If your application does not have the android:debuggable flag set in its manifest, the server returned by this method will be a dummy object that does not do anything. This allows you to use the same code in debug and release versions of your application.

Parameters
context A Context used to check whether the application is debuggable, this can be the application context

public boolean isRunning ()

Indicates whether the server is currently running.

Returns
  • True if the server is running, false otherwise.
See Also

public void removeWindow (Activity activity)

Invoke this method to unregister a view hierarchy.

Parameters
activity The activity whose view hierarchy/window to unregister

public void removeWindow (View view)

Invoke this method to unregister a view hierarchy.

Parameters
view A view that belongs to the view hierarchy/window to unregister

public void run ()

Main server loop.

public void setFocusedWindow (View view)

Invoke this method to change the currently focused window.

Parameters
view A view that belongs to the view hierarchy/window that has focus, or null to remove focus

public void setFocusedWindow (Activity activity)

Invoke this method to change the currently focused window.

Parameters
activity The activity whose view hierarchy/window hasfocus, or null to remove focus

public boolean start ()

Starts the server.

Returns
  • True if the server was successfully created, or false if it already exists.
Throws
IOException If the server cannot be created.
See Also

public boolean stop ()

Stops the server.

Returns
  • True if the server was stopped, false if an error occurred or if the server wasn't started.
See Also