模拟音频焦点抢占策略,实现地图焦点抢占功能

一个焦点抢占策略的代码示例

包含以下功能:
requestMapFocus;
abandonMapFocus;
registerMapFocusChangeListener;
onMapFocusChanged(int previousMap, int focusMap);

主体代码:

package com.test;

import java.util.LinkedList;

public class MapFocusManager {

    private OnMapFocusChangeListener mMapFocusChangeListener;
    private LinkedList<Integer> mFocusedMapList = new LinkedList<>();

    public synchronized boolean requestMapFocus(int mapType) {
        if (mapType < 0) {
            // Log.w("TAG", "illegal mapType:" + mapType);
            return false;
        }

        int currentFocusMap = -1;
        int index = mFocusedMapList.indexOf(mapType);
        if (index == 0) {
            // already at first, do nothing.
            return true;
        } else if (index > 0) {
            // already exit mFocusedMapList, but not at first.
            currentFocusMap = mFocusedMapList.get(0);
            mFocusedMapList.remove(index);
            mFocusedMapList.addFirst(mapType);
            mMapFocusChangeListener.onMapFocusChanged(currentFocusMap, mapType);
        } else {
            // not exist in mFocusedMapList;
            if (0 == mFocusedMapList.size()) {
                currentFocusMap = -1;
            } else {
                currentFocusMap = mFocusedMapList.get(0);
            }
            mFocusedMapList.addFirst(mapType);
            mMapFocusChangeListener.onMapFocusChanged(currentFocusMap, mapType);
        }
        return true;
    }

    public synchronized boolean abandonMapFocus(int mapType) {
        int index = mFocusedMapList.indexOf(mapType);
        if (index == 0) {
            // at first, remove it!
            mFocusedMapList.removeFirst();
            int newFocusMap = -1;
            if (0 == mFocusedMapList.size()) {
                newFocusMap = -1;
            } else {
                newFocusMap = mFocusedMapList.getFirst();
            }
            mMapFocusChangeListener.onMapFocusChanged(mapType, newFocusMap);
            return true;
        } else if (index > 0) {
            // already exist in mFocusedMapList, but not first, just remove it, focus not changed.
            mFocusedMapList.remove(index);
        } else {
            // not exist in mFocusedMapList;
            return false;
        }
        return true;
    }

    public synchronized void registerMapFocusChangeListener(OnMapFocusChangeListener listener) {
        mMapFocusChangeListener = listener;
    }
}

监听器代码:

package com.test;

public interface OnMapFocusChangeListener {
    void onMapFocusChanged(int previousMap, int focusMap);
}

测试程序和输出

    @Test
    public void test_demo() {
        MapFocusManager mapFocusManager = new MapFocusManager();
        OnMapFocusChangeListener listener = new OnMapFocusChangeListener() {
            @Override
            public void onMapFocusChanged(int previousMap, int focusMap) {
                System.out.println("onMapFocusChanged, previousMap:" + previousMap
                        + ", focusMap:" + focusMap);
            }
        };
        mapFocusManager.registerMapFocusChangeListener(listener);

        System.out.println("start abandon 123");
        mapFocusManager.abandonMapFocus(1);
        mapFocusManager.abandonMapFocus(2);
        mapFocusManager.abandonMapFocus(3);
        System.out.println("start request 1");
        mapFocusManager.requestMapFocus(1);
        System.out.println("start abandon 1");
        mapFocusManager.abandonMapFocus(1);

        System.out.println("start request 123");
        mapFocusManager.requestMapFocus(1);
        mapFocusManager.requestMapFocus(2);
        mapFocusManager.requestMapFocus(3);
        System.out.println("start abandon 2");
        mapFocusManager.abandonMapFocus(2);

        System.out.println("start abandon 3");
        mapFocusManager.abandonMapFocus(3);

        System.out.println("start abandon 1");
        mapFocusManager.abandonMapFocus(1);

        System.out.println("start abandon 1");
        mapFocusManager.abandonMapFocus(1);

        System.out.println("start abandon 1");
        mapFocusManager.abandonMapFocus(1);
    }
start abandon 123
start request 1
onMapFocusChanged, previousMap:-1, focusMap:1
start abandon 1
onMapFocusChanged, previousMap:1, focusMap:-1
start request 123
onMapFocusChanged, previousMap:-1, focusMap:1
onMapFocusChanged, previousMap:1, focusMap:2
onMapFocusChanged, previousMap:2, focusMap:3
start abandon 2
start abandon 3
onMapFocusChanged, previousMap:3, focusMap:1
start abandon 1
onMapFocusChanged, previousMap:1, focusMap:-1
start abandon 1
start abandon 1

Process finished with exit code 0

经测试,功能符合期望;

有以下问题,需要完善:
1,listener=null状态未判断;
2,mFocusedMapList越界情况未考虑;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值