当前位置: 移动技术网 > IT编程>移动开发>Android > Android6.0 源码修改之 Contacts应用

Android6.0 源码修改之 Contacts应用

2019年04月26日  | 移动技术网IT编程  | 我要评论

仕途风流txt下载,泥鳅养殖利润,豪门恋人

一、contacts应用的主界面和联系人详情界面增加顶部菜单添加退出按钮

通过hierarchy view 工具可以发现

主界面对应的类为 peopleactivity

联系人详情界面对应的类为 quickcontactactivity

左上角的退出按钮其实很简单,系统actionbar已经帮我们实现了这一功能,只是没有显示出来而已。在oncreate()方法中,在setcontentview()方法之后,添加如下代码即可显示返回的箭头

    actionbar mactionbar = getactionbar();
    if (mactionbar != null) {
        log.i(tag, "getsupportactionbar != null....");
        mactionbar.setdisplayhomeasupenabled(true);
        mactionbar.sethomebuttonenabled(true);
    }

接下来在onoptionsitemselected()中监听返回按钮的事件即可

@override
public boolean onoptionsitemselected(menuitem item) {
    switch (item.getitemid()) {
        case android.r.id.home: {
                 finish();
            }
            return true;
        }
        ....
}

图1 左上角返回退出功能

二、第三方app拉起主界面时直接显示模糊查询对应的联系人列表

简单分析一下,模糊查询需要对应的查询联系人名称,可以通过intent传递参数,这里定义为string类型,当传递参数不为null时,模拟手动点击搜索框对应的逻辑。如下在 peopleactivity 的 oncreate()方法中增加获取参数的代码

final string querystring = getintent().getstringextra("querystring");
    if (!textutils.isempty(querystring)) {
         new handler().postdelayed(new runnable() {
            @override
            public void run() {
                showquerytextfragment(querystring);
            }
        }, 100);//让搜索逻辑延迟100ms执行
    }

通过测试发现,不加延迟触发搜索框对应的逻辑并不会显示模糊查询结果界面。接下来我们分析点击搜索框对应的逻辑代码,找到搜索框对应的控件id,menu_search, 回到刚刚的菜单监听方法 onoptionsitemselected()中

@override
public boolean onoptionsitemselected(menuitem item) {
    ...

    switch (item.getitemid()) {
     case r.id.menu_search: {
            onsearchrequested();
            return true;
        }
    }
    ...
}

@override
public boolean onsearchrequested() { // search key pressed.
    log.d(tag, "[onsearchrequested]");
    //不在搜索模式下,也就是没有点击过搜索框
    if (!mactionbaradapter.isselectionmode()) {
        //获取焦点,弹出键盘
        mactionbaradapter.setsearchmode(true);
    }
    return true;
}

从上面不难看出最终调用 mactionbaradapter 的方法,我们接着跟进去

源码位置 packages/apps/contacts/src/com/android/contacts/activities/actionbaradapter.java

public void setsearchmode(boolean flag) {
    if (msearchmode != flag) {
        msearchmode = flag;
        update(false /* skipanimation */);
        if (msearchview == null) {
            return;
        }
        if (msearchmode) {
            msearchview.setenabled(true);
            setfocusonsearchview();
        } else {
            // disable search view, so that it doesn't keep the ime visible.
            msearchview.setenabled(false);
        }
        setquerystring(null);
    } else if (flag) {
        // everything is already set up. still make sure the keyboard is up
        //需要注释此处,不然多次调用并退出再次拉起容易出现键盘弹出的情况
        //if (msearchview != null) setfocusonsearchview();
    }
}

public void setfocusonsearchview() {
    //msearchview获取焦点(先获取焦点才能弹出键盘)
    msearchview.requestfocus();
    //弹出键盘
    showinputmethod(msearchview); // workaround for the "ime not popping up" issue.
}

private void showinputmethod(view view) {
    final inputmethodmanager imm = (inputmethodmanager) mactivity.getsystemservice(
            context.input_method_service);
    if (imm != null) {
        imm.showsoftinput(view, 0);
    }
}

看到这里我们可以猜想到 msearchview 肯定设置了文字改变监听,继续查找 addtextchangedlistener

...
msearchview.setinputtype(editorinfo.type_class_text
        | editorinfo.type_text_variation_email_address);
msearchview.addtextchangedlistener(new searchtextwatcher());
...

private class searchtextwatcher implements textwatcher {

    @override
    public void ontextchanged(charsequence querystring, int start, int before, int count) {
        if (querystring.equals(mquerystring)) {
            return;
        }
        //当前输入的模糊查询的名称
        mquerystring = querystring.tostring();
        if (!msearchmode) {
            if (!textutils.isempty(querystring)) {
                setsearchmode(true);
            }
        } else if (mlistener != null) {
            //回调通知 peopleactivity 改变界面
            mlistener.onaction(action.change_search_query);
        }
        mclearsearchview.setvisibility(
                textutils.isempty(querystring) ? view.gone : view.visible);
    }

    @override
    public void aftertextchanged(editable s) {}

    @override
    public void beforetextchanged(charsequence s, int start, int count, int after) {}
}

回到 peopleactivity 中找到监听 action.change_search_query 的代码如下

 @override
public void onaction(int action) {
    log.d(tag,"[onaction]action = " + action);
    /// m: [vcs] @{
    if (mvcscontroller != null) {
        mvcscontroller.onactionvcs(action);
    }
    /// @}
    switch (action) {
    ...
    case actionbaradapter.listener.action.change_search_query:
            //获取当前输入的模糊查询姓名
            final string querystring = mactionbaradapter.getquerystring();
            //显示对应的fragment
            setquerytexttofragment(querystring);
            updatedebugoptionsvisibility(
                    enable_debug_options_hidden_code.equals(querystring));
            break;
        default:
            throw new illegalstateexception("unkonwn actionbaradapter action: " + action);
    }
}

到此,搜索框模糊查询对应的逻辑就分析完了,那么我们就模拟调用对应的逻辑就ok了,再来把整体流程捋一遍,

点击搜索框->获取焦点->弹出键盘->输入姓名->收到文字内容改变的监听->将输入的内容回调给 peopleactivity->收到回调显示对应的结果fragment

好了,通过调用edittext.settext()方法也能触发文字内容改变的监听,前提是要先获取焦点,那么我们的 showquerytextfragment() 实现如下

private void showquerytextfragment(string querystring){
    log.d(tag, "[showquerytextfragment]");
    if (!mactionbaradapter.isselectionmode()) {
        log.e(tag, "[querystring==]"+querystring);
        mactionbaradapter.setsearchmode(true);
        mactionbaradapter.setquerystring(querystring);
    }
}


图2 第三方app拉起主界面显示对应的联系人

三、第三方app拉起联系人详情界面只滑动到一半显示的问题

-

图3 拉起只显示一半

图4 拉起完全显示

首先从系统的联系人列表界面点击进入详情界面是能完整显示的,所以猜想应该是传递的参数不太一样。所以还是从oncreate()方法看下来

public class quickcontactactivity extends contactsactivity {

        /**
     * quickcontacts immediately takes up the full screen. all possible information is shown.
     * this value for {@link android.provider.contactscontract.quickcontact#extra_mode}
     * should only be used by the contacts app.
     */
    public static final int mode_fully_expanded = 4;
    //看上面的注释就知道了肯定是跟这个变量有关系, 立刻显示全屏,应当只用于 联系人 app 使用

     @override
    protected void oncreate(bundle savedinstancestate) {
        trace.beginsection("oncreate()");
        super.oncreate(savedinstancestate);

        if (requestpermissionsactivity.startpermissionactivity(this)) {
            return;
        }

        getwindow().setstatusbarcolor(color.transparent);
        //处理intent传递的参数
        processintent(getintent());

        .....
        //scroller初始化,传递滚动模式
        mscroller.initialize(mmultishrinkscrollerlistener, mextramode == mode_fully_expanded);
        // mscroller needs to perform asynchronous measurements after initalize(), therefore
        // we can't mark this as gone.
        mscroller.setvisibility(view.invisible);
        ...
    }

    private void processintent(intent intent) {
        ...
        //获取传递的extra_mode,不传默认为large,查看api对应的int值为3,mode_fully_expanded为4, 所以不传递参数或者参数对应值不为4就只显示半屏
        mextramode = getintent().getintextra(quickcontact.extra_mode, quickcontact.mode_large);
        
        ...
    }

}

通过上面的分析 intent需要传递 quickcontact.extra_mode 参数, 当你点进去 quickcontact中发现并没有对应4的变量(猜想应该是留了一手不让第三方app直接全屏显示)

正确的打开姿势

private void gotocontact(){
    uri personuri = contenturis.withappendedid(contacts.people.content_uri, 1);
    intent intent = new intent();
    intent.setaction(intent.action_view);
    intent.setdata(personuri);
    //这句比较关键
    intent.putextra(contactscontract.quickcontact.extra_mode, 4);
    intent.setflags(intent.flag_activity_new_task);
    startactivity(intent);
}

总结

1、话说当把navigationbar去掉以后,给每个activity添加返回按钮是个很麻烦的工作,可以借鉴一下苹果的思路,直接在屏幕(window)中添加一个悬浮的按钮处理返回点击事件。具体实现可以看这篇android6.0 源码修改之 仿ios添加全屏可拖拽浮窗返回按钮

2、源码没那么可怕,干起来。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网