android上的i-jetty (2)用Servlet显示Android设备的app列表
环境 : android + i-jetty + servlet
MyServlet.java
1 package com.synnex.servlet;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.util.List;
7
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletException;
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import android.content.Context;
16 import android.content.Intent;
17 import android.content.pm.ApplicationInfo;
18 import android.content.pm.PackageInfo;
19 import android.content.pm.PackageManager;
20 import android.content.pm.PackageManager.NameNotFoundException;
21 import android.content.pm.ResolveInfo;
22 import android.graphics.Bitmap;
23 import android.graphics.Canvas;
24 import android.graphics.PixelFormat;
25 import android.graphics.drawable.Drawable;
26 import android.util.Log;
27
28 public class MyServlet extends HttpServlet
29 {
30 private static final long serialVersionUID = -4258280559794945071L;
31 private final String TAG = "MyServlet";
32 private final String METHOD = "method";
33
34 private Context context;
35
36 public void init(ServletConfig config) throws ServletException
37 {
38 super.init(config);
39 context = (Context)config.getServletContext().getAttribute("org.mortbay.ijetty.context");
40 Log.v(TAG, "## init android context success ##");
41 }
42
43 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
44 {
45 String method = request.getParameter(METHOD);
46 if("listApp".equals(method))
47 {
48 response.setContentType("text/html; charset=UTF-8");
49 listApp(request, response);
50 }
51 else if("appIcon".equals(method))
52 {
53 appIcon(request, response);
54 }
55 else
56 {
57 response.setContentType("text/html; charset=UTF-8");
58 sayHello(request, response);
59 }
60 }
61
62 private void appIcon(HttpServletRequest request, HttpServletResponse response) throws IOException
63 {
64 response.setContentType("image/JPEG");
65 ServletOutputStream out = response.getOutputStream();
66 PackageManager pm = context.getPackageManager();
67 try
68 {
69 Drawable icon = pm.getApplicationIcon(request.getParameter("id"));
70 ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 drawableToBitmap(icon).compress(Bitmap.CompressFormat.PNG, 100, baos);
72 byte[] b = baos.toByteArray();
73 out.write(b);
74 out.flush();
75 }
76 catch (NameNotFoundException e)
77 {
78 Log.e(TAG, "get app icon error : ", e);
79 out.write("error".getBytes("UTF-8"));
80 }
81 response.flushBuffer();
82 }
83
84 private void sayHello(HttpServletRequest request, HttpServletResponse response) throws IOException
85 {
86 PrintWriter writer = response.getWriter();
87 writer.write("Hello World!!!");
88 writer.flush();
89 response.flushBuffer();
90 }
91
92 private void listApp(HttpServletRequest request, HttpServletResponse response) throws IOException
93 {
94 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
95 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
96 List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(mainIntent, 0);
97 PackageManager pm = context.getPackageManager();
98
99 StringBuilder buf = new StringBuilder(500);
100 buf.append("<table>")
101 .append("<tr>")
102 .append("<th>App Name</th>")
103 .append("<th>App Version</th>")
104 .append("<th>Package Name</th>")
105 .append("<th>App Icon</th>")
106 .append("</tr>");
107 String imgBaseUrl = request.getContextPath() + request.getServletPath() + "?" + METHOD + "=appIcon";
108 Log.v(TAG, imgBaseUrl);
109 for(ResolveInfo info : apps)
110 {
111 try
112 {
113 PackageInfo appInfo = pm.getPackageInfo(info.activityInfo.packageName, 0);
114 if((appInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)
115 {
116 buf.append("<tr>")
117 .append("<td>").append(appInfo.applicationInfo.loadLabel(pm)).append("</td>")
118 .append("<td>").append(appInfo.versionName).append("</td>")
119 .append("<td>").append(appInfo.packageName).append("</td>")
120 .append("<td><img width='50px' height='50px' src='").append(imgBaseUrl).append("&id=").append(appInfo.packageName).append("'/></td>")
121 .append("<td></td>")
122 .append("</tr>");
123 }
124 }
125 catch (NameNotFoundException e)
126 {
127 Log.e(TAG, "get app info error: ", e);
128 continue;
129 }
130 }
131
132 buf.append("</table>");
133
134 PrintWriter writer = response.getWriter();
135 writer.write(buf.toString());
136 writer.flush();
137 response.flushBuffer();
138 }
139
140 private static Bitmap drawableToBitmap(Drawable drawable)
141 {
142 int w = drawable.getIntrinsicWidth();
143 int h = drawable.getIntrinsicHeight();
144
145 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
146 Bitmap bitmap = Bitmap.createBitmap(w, h, config);
147 Canvas canvas = new Canvas(bitmap);
148 drawable.setBounds(0, 0, w, h);
149 drawable.draw(canvas);
150 return bitmap;
151 }
152 }
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.util.List;
7
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletException;
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import android.content.Context;
16 import android.content.Intent;
17 import android.content.pm.ApplicationInfo;
18 import android.content.pm.PackageInfo;
19 import android.content.pm.PackageManager;
20 import android.content.pm.PackageManager.NameNotFoundException;
21 import android.content.pm.ResolveInfo;
22 import android.graphics.Bitmap;
23 import android.graphics.Canvas;
24 import android.graphics.PixelFormat;
25 import android.graphics.drawable.Drawable;
26 import android.util.Log;
27
28 public class MyServlet extends HttpServlet
29 {
30 private static final long serialVersionUID = -4258280559794945071L;
31 private final String TAG = "MyServlet";
32 private final String METHOD = "method";
33
34 private Context context;
35
36 public void init(ServletConfig config) throws ServletException
37 {
38 super.init(config);
39 context = (Context)config.getServletContext().getAttribute("org.mortbay.ijetty.context");
40 Log.v(TAG, "## init android context success ##");
41 }
42
43 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
44 {
45 String method = request.getParameter(METHOD);
46 if("listApp".equals(method))
47 {
48 response.setContentType("text/html; charset=UTF-8");
49 listApp(request, response);
50 }
51 else if("appIcon".equals(method))
52 {
53 appIcon(request, response);
54 }
55 else
56 {
57 response.setContentType("text/html; charset=UTF-8");
58 sayHello(request, response);
59 }
60 }
61
62 private void appIcon(HttpServletRequest request, HttpServletResponse response) throws IOException
63 {
64 response.setContentType("image/JPEG");
65 ServletOutputStream out = response.getOutputStream();
66 PackageManager pm = context.getPackageManager();
67 try
68 {
69 Drawable icon = pm.getApplicationIcon(request.getParameter("id"));
70 ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 drawableToBitmap(icon).compress(Bitmap.CompressFormat.PNG, 100, baos);
72 byte[] b = baos.toByteArray();
73 out.write(b);
74 out.flush();
75 }
76 catch (NameNotFoundException e)
77 {
78 Log.e(TAG, "get app icon error : ", e);
79 out.write("error".getBytes("UTF-8"));
80 }
81 response.flushBuffer();
82 }
83
84 private void sayHello(HttpServletRequest request, HttpServletResponse response) throws IOException
85 {
86 PrintWriter writer = response.getWriter();
87 writer.write("Hello World!!!");
88 writer.flush();
89 response.flushBuffer();
90 }
91
92 private void listApp(HttpServletRequest request, HttpServletResponse response) throws IOException
93 {
94 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
95 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
96 List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(mainIntent, 0);
97 PackageManager pm = context.getPackageManager();
98
99 StringBuilder buf = new StringBuilder(500);
100 buf.append("<table>")
101 .append("<tr>")
102 .append("<th>App Name</th>")
103 .append("<th>App Version</th>")
104 .append("<th>Package Name</th>")
105 .append("<th>App Icon</th>")
106 .append("</tr>");
107 String imgBaseUrl = request.getContextPath() + request.getServletPath() + "?" + METHOD + "=appIcon";
108 Log.v(TAG, imgBaseUrl);
109 for(ResolveInfo info : apps)
110 {
111 try
112 {
113 PackageInfo appInfo = pm.getPackageInfo(info.activityInfo.packageName, 0);
114 if((appInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)
115 {
116 buf.append("<tr>")
117 .append("<td>").append(appInfo.applicationInfo.loadLabel(pm)).append("</td>")
118 .append("<td>").append(appInfo.versionName).append("</td>")
119 .append("<td>").append(appInfo.packageName).append("</td>")
120 .append("<td><img width='50px' height='50px' src='").append(imgBaseUrl).append("&id=").append(appInfo.packageName).append("'/></td>")
121 .append("<td></td>")
122 .append("</tr>");
123 }
124 }
125 catch (NameNotFoundException e)
126 {
127 Log.e(TAG, "get app info error: ", e);
128 continue;
129 }
130 }
131
132 buf.append("</table>");
133
134 PrintWriter writer = response.getWriter();
135 writer.write(buf.toString());
136 writer.flush();
137 response.flushBuffer();
138 }
139
140 private static Bitmap drawableToBitmap(Drawable drawable)
141 {
142 int w = drawable.getIntrinsicWidth();
143 int h = drawable.getIntrinsicHeight();
144
145 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
146 Bitmap bitmap = Bitmap.createBitmap(w, h, config);
147 Canvas canvas = new Canvas(bitmap);
148 drawable.setBounds(0, 0, w, h);
149 drawable.draw(canvas);
150 return bitmap;
151 }
152 }
在浏览器中输入:http://{android_ip}:8080/MyServlet?method=listApp
显示结果: