现在的位置: 首页 > 综合 > 正文

4.2 开发者选项–”电源错误报告”的适配

2013年10月26日 ⁄ 综合 ⁄ 共 6299字 ⁄ 字号 评论关闭

这个应该属于系统适配,说难不难,说容易不容易。一些平台,如三星、TI等google原生已经进行适配,而一些平台则需要自己适配,比如我们公司就需要我们适配

主要从几个方面来搞定这个问题,以三星为例:

【一、流程分析】

通过一路坎坷分析发现,调用流程是这样的

通过bugreport调用bugmailer.sh,进行截屏和保存bugreport信息,调用send_bug去发送这个错误信息,因为是测试,获取的是本地登录的邮箱。

bugmailer.sh

#!/system/bin/sh

# TODO: restructure this to keep bugreports entirely on internal storage

# Do not allow bugreports on user builds unless USB debugging
# is enabled.
if [ "x$(getprop ro.build.type)" = "xuser" -a \
     "x$(getprop init.svc.adbd)" != "xrunning" ]; then
  exit 0
fi

# Build emulated storage paths when appropriate
# See storage config details at http://source.android.com/tech/storage/
if [ -n "$EMULATED_STORAGE_SOURCE" ]; then
  writePath="$EMULATED_STORAGE_SOURCE/0"
  readPath="$EMULATED_STORAGE_TARGET/0"
else
  writePath="$EXTERNAL_STORAGE"
  readPath="$EXTERNAL_STORAGE"
fi

tmpPath="/data/local/tmp"
bugreportPath="bugreports"
screenshotPath="Pictures/Screenshots"

# Create directories if needed
if [ ! -e "$writePath/$bugreportPath" ]; then
  mkdir "$writePath/$bugreportPath"
fi
if [ ! -e "$writePath/$screenshotPath" ]; then
  mkdir "$writePath/$screenshotPath"
fi

timestamp=`date +'%Y-%m-%d-%H-%M-%S'`

# take screen shot
# we run this as a bg job in case screencap is stuck
/system/bin/screencap -p "$writePath/$screenshotPath/Screenshot_$timestamp.png" &

# run bugreport
/system/bin/dumpstate -o "$tmpPath/bugreport-$timestamp" $@

# copy finished bugreport into place for sending
cp "$tmpPath/bugreport-$timestamp.txt" "$writePath/$bugreportPath/bugreport-$timestamp.txt"
# clean up any remaining files
rm $tmpPath/bugreport*

# invoke send_bug to look up email accounts and fire intents
# make it convenient to send bugreport to oneself
/system/bin/send_bug "$readPath/$bugreportPath/bugreport-$timestamp.txt" "$readPath/$screenshotPath/Screenshot_$timestamp.png"

send_bug

# Script to start "send_bug" on the device
#
base=/system
export CLASSPATH=$base/framework/send_bug.jar
exec app_process $base/bin com.android.commands.sendbug.SendBug "$@"

【二、对所需要的工具进行预制】

1.进行资源预置

\device\samsung\tuna\device.mk

# for bugmailer
PRODUCT_PACKAGES += send_bug
PRODUCT_COPY_FILES += \
	system/extras/bugmailer/bugmailer.sh:system/bin/bugmailer.sh \
	system/extras/bugmailer/send_bug:system/bin/send_bug

2.加入启动项目

\device\samsung\tuna\init.tuna.rc

# bugreport is triggered by holding down volume down, volume up and power
service bugreport /system/bin/bugmailer.sh -v
	class main
	disabled
	oneshot
	keycodes 114 115 116

3.添加资源预置

配置编译环境,编译\system\extras\bugmailer下的文件,将编译出来的send_bug.jar和send_bug.odex拷贝至\system\extras\bugmailer目录,并对其进行预制,这里可以和进行资源预制合二为一,修改如下:

\device\samsung\tuna\device.mk

# for bugmailer
PRODUCT_PACKAGES += send_bug
PRODUCT_COPY_FILES += \
	system/extras/bugmailer/bugmailer.sh:system/bin/bugmailer.sh \
	system/extras/bugmailer/send_bug:system/bin/send_bug \
        system/extras/bugmailer/send_bug.jar:system/bin/send_bug.jar \
        system/extras/bugmailer/send_bug.odex:system/bin/send_bug.odex

【三、调整获取Email的策略】

通过分析,发现通过send_bug会调用到SendBug.class中。

到这个类后的基本流程是,在main方法中开启一个线程去完成这个事,在线程中会调用tryBugReporter查看系统中是否有与其自定义的intent匹配的activity,正常的情况下肯定是木有的啦。此时会获取本地的登录的邮件,如果有登录邮件帐号,弹出发送邮件的界面,采集到的数据以附件的形式附加,如果没有登录邮件帐号,则什么都不做。此处我们可以修改策略,如果没有登陆邮箱,则跳转至登录邮箱的界面。

Intent data=new Intent();
data.setClassName("com.android.email", "com.android.email.activity.setup.AccountSetupBasics");
startActivity(data); 

PS:这里不要使用action的方法,死活调用不成功不清楚到底是什么原因。只要用action的方式就会报错,activityNotFount

【更新】

这里这种方式是不能跳转过去的,代码中使用的跳转方式是如下(一直没更新,呵呵):

system\extra\bugmail\src\com\android\commands\sendbug\SendBug.java

    private void run(String bugreportPath, String screenShotPath) {
        final File bugreport = new File(bugreportPath);
        File screenShot = null;
        if (screenShotPath != null) {
            screenShot = new File(screenShotPath);
        }
        final Uri bugreportUri = Uri.fromFile(bugreport);
        // todo (aalbert): investigate adding a screenshot to BugReporter
        Intent intent = tryBugReporter(bugreportUri);
        if (intent == null) {
            final Uri screenshotUri = screenShot != null
                    ? Uri.fromFile(screenShot) : null;
            intent = getSendMailIntent(bugreportUri, screenshotUri);
        }
        final IActivityManager am = ActivityManagerNative.getDefault();
        if (am == null) {
             Log.e(LOG_TAG, "Cannot get ActivityManager, is the system running?");
             return;
        }
        if (intent != null) {
            try {
                am.startActivityAsUser(null, intent, intent.getType(), null, null, 0, 0,
                        null, null, null, UserHandle.USER_CURRENT);
            } catch (RemoteException e) {
                // ignore
            }
        } else {
            Log.w(LOG_TAG, "Cannot find account to send bugreport, local path: "
                    + bugreportPath);
            try {
                 Intent sendMail = new Intent();
                 sendMail.setClassName("com.android.email", "com.android.email.activity.setup.AccountSetupBasics");
                 am.startActivityAsUser(null, sendMail, null, null, null, 0, 0,
                        null, null, null, UserHandle.USER_CURRENT);
            } catch (RemoteException e) {
                // ignore
            }
        }
    }

【四、整理邮箱发送资料】

该部分系数网友整理,家里写代码的环境崩了,一直懒得去折腾,声明如有侵权,立刻清除该部分

在Android中,调用Email有三种类型的Intent:

Intent.ACTION_SENDTO 无附件的发送
Intent.ACTION_SEND 带附件的发送
Intent.ACTION_SEND_MULTIPLE 带有多附件的发送

当然,所谓的调用Email,只是说Email可以接收Intent并做这些事情,可能也有其他的应用程序实现了相关功能,所以在执行的时候,会出现选择框进行选择。

1.使用SENTTO发送

Intent data=new Intent(Intent.ACTION_SENDTO);  
data.setData(Uri.parse("mailto:455245521@qq.com"));  
data.putExtra(Intent.EXTRA_SUBJECT, "这是标题");  
data.putExtra(Intent.EXTRA_TEXT, "这是内容");  
startActivity(data); 

通过向Intent中putExtra来设定邮件的相关参数。

2.使用SEND发送

Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { "fdafdafa@gmail.com" }; 
String[] ccs = { "gegeff@gmail.com" }; 
String[] bccs = {"fdafda@gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Chrysanthemum.jpg"));
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);

很简单,发送邮件中,有收件者,抄送者,密送者。 也就是分别通过
Intent.EXTRA_EMAIL,
Intent.EXTRA_CC,
Intent.EXTRA_BCC
来进行putExtra来设定的。

而单个附件的发送,则使用Intent.EXTRA_STREAM来设置附件的地址Uri。

3.使用SEND_MULTIPLE来进行多附件的发送

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String[] tos = { "wingfourever@gmail.com" }; 
String[] ccs = { "tongyue@gmail.com" }; 
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.parse("file:///sdcard/Chrysanthemum.jpg"));
imageUris.add(Uri.parse("file:///sdcard/Desert.jpg"));		
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);

发送多个附件,最主要的时候,通过putParcelableArrayListExtra将多个附件的Uri地址List设置进去就OK了。

端午节快乐!!愿各位心想事成,开心快乐每一天!!

抱歉!评论已关闭.