通过响铃、摘机和空闲在android上检测更好的呼叫状态

ssgvzors  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(357)

一周前,一周来我一直在尝试一个完全独立的应用程序来检测通话状态。我在吃棒棒糖5.1,所以我不能用 PRECISE_CALL_STATE 存在于棉花糖上面。我被限制在通常的通话状态,关机和空闲。
在google的帮助下,我进行了广泛的研究(这里的大部分是stackoverflow),这让我意识到,如果没有lollipop或更低版本上的这3个,或者没有新的android版本上的系统权限,可能就没有其他检测呼叫状态的方法了。所有固件上都有非常精确的呼叫状态。但是从这里的一个线程来看,它们似乎只能由当前的手机应用程序使用(我不会替换它,它仍然是普通的手机应用程序)。所以在这种情况下,似乎没有办法使用这些状态。
我也写了这个问题,并立即回答它,因为我能够使一些工作,只是对我的应用程序,我正在做的罚款。如果它是一个手机应用程序,那么它必须更加精确。但就我而言,我一点也不介意。
当我看到这么多关于这个问题的问题时,我决定把所有的问题都放在同一个问题里,用我努力做的事情来回答,对我来说已经足够好了,对其他人来说也是如此。请随时提出改进建议和/或发布其他解决方案!
如果我在提问和回答上有任何错误,请纠正我。我以前从没这么做过。

soat7uwm

soat7uwm1#

考虑到我写的问题,我被迫使用这3种状态,或者使用调用历史记录。由于我希望应用程序尽可能独立,我试图让应用程序检测到 PRECISE_CALL_STATE 从它自己开始。除非电话太多。如果它们是3个或更多,我必须从调用历史记录中获取一些调用的状态(如果有3个调用,则有一个情况除外,代码中对此进行了解释)。很遗憾,我就是没法检测到有人在等电话。
到目前为止,我可以发现以下情况:
来电;
来电等待;
去话呼叫;
电话刚刚丢失;
前段时间打丢了电话;
刚接电话;
刚才接的电话;
通话刚刚结束;
电话一段时间前就结束了。
这是我为实现这一点而编写的代码。顺便说一下,因为这已经有一些行了,而且我发现stack exchange网络网站有一个自动许可证,然后将这个添加到它(正如我在个人资料中所说的),这样任何人都可以用它做任何他们想做的事,包括复制粘贴(可能需要时间来理解,也可能不需要时间来理解,因为当我第一次这样做时没有任何评论,我很难理解我做了什么
这项工作标记为cc0 1.0通用。要查看此许可证的副本,请访问https://creativecommons.org/publicdomain/zero/1.0
希望这是大到足以担心许可证问题。一周前我看到了这个,我开始思考什么时候在我发布的代码上添加cc0的许可通知。如果这里没有必要,请告诉我,以便我下次改进“检测”。

public static final String CALL_PHASE_OUTGOING = "CALL_PHASE_OUTGOING";
public static final String CALL_PHASE_RINGING_NEW = "CALL_PHASE_RINGING_NEW";
public static final String CALL_PHASE_LOST = "CALL_PHASE_LOST";
public static final String CALL_PHASE_LOST_LATE = "CALL_PHASE_LOST_LATE";
public static final String CALL_PHASE_RINGING_WAITING = "CALL_PHASE_RINGING_WAITING";
//public static final String CALL_PHASE_ON_HOLD = "CALL_PHASE_ON_HOLD";
public static final String CALL_PHASE_ANSWERED = "CALL_PHASE_ANSWERED";
public static final String CALL_PHASE_ANSWERED_LATE = "CALL_PHASE_ANSWERED_LATE";
public static final String CALL_PHASE_FINISHED = "CALL_PHASE_FINISHED";
public static final String CALL_PHASE_FINISHED_LATE = "CALL_PHASE_FINISHED_LATE";

public static final String BETTER_CALL_STATE_OUTGOING = "BETTER_CALL_STATE_OUTGOING";
public static final String BETTER_CALL_STATE_INCOMING = "BETTER_CALL_STATE_INCOMING";
public static final String BETTER_CALL_STATE_WAITING = "BETTER_CALL_STATE_WAITING";
public static final String BETTER_CALL_STATE_DISCONNECTED = "BETTER_CALL_STATE_FINISHED";
//public static final String BETTER_CALL_STATE_ON_HOLD = "BETTER_CALL_STATE_ON_HOLD";
public static final String BETTER_CALL_STATE_ACTIVE = "BETTER_CALL_STATE_ACTIVE";
/**
 * <p>This gets the phase of the call when a new phone state is detected (RINGING, OFFHOOK, or IDLE).</p>
 * <p>There are values ending in "_LATE". Those are so because they're only detected after the end of all calls are over and after the phone gets to IDLE state.
 *    Which means, they already happened some time ago (1 second, 10 minutes, unpredictable).</p>
 * <br>
 * <p><b><u>---CONSTANTS---</u></b></p>
 * <p>- <u>CALL_PHASE_OUTGOING [int]</u> --> returned in case an outgoing call was just started (whether it is answered or not by the other party - it's not possible to detect that easily).</p>
 * <p>- <u>CALL_PHASE_RINGING_NEW [int]</u> --> returned in case it's a new incoming call.</p>
 * <p>- <u>CALL_PHASE_LOST [int]</u> --> returned in case the call has just been lost.</p>
 * <p>- <u>CALL_PHASE_LOST_LATE [int]</u> --> returned in case the call was lost some time ago already.</p>
 * <p>- <u>CALL_PHASE_RINGING_WAITING [int]</u> --> returned in case there's a new call which is waiting to be answered (some call is already active).</p>
 * <p>- <u>CALL_PHASE_ANSWERED [int]</u> --> returned in case the call has just been answered.</p>
 * <p>- <u>CALL_PHASE_ANSWERED_LATE [int]</u> --> returned in case the call was answered some time ago alreaedy.</p>
 * <p>- <u>CALL_PHASE_FINISHED [int]</u> --> returned in case the call was just finished (after having been answered - if it wasn't answered, it was LOST or LOST_LATE).</p>
 * <p>- <u>CALL_PHASE_FINISHED_LATE [int]</u> --> returned in case the call was finished some time ago already (the same in parenthesis for FINISHED applies here).</p>
 * <p><b><u>---CONSTANTS---</u></b></p>
 *
 * @param context <u>[Context]</u> --> Context of the application.
 * @param state <u>[int]</u> --> One of the CALL_STATE in TelephonyManager.
 * @param incomingNumber <u>[String]</u> --> Phone number that came with the state change.
 * @param calls_state <u>[ArrayList(ArrayList(String))]</u> --> An ArrayList of the indicated type that will have the list of calls currently in processing
 *                        (put empty in the beginning and keep the object where the array was created in memory, so the contents of the array are kept, or save it somewhere,
 *                        but don't give always an empty one - this method handles cleaning it when needed. Just give it empty in the beginning of the app and let the method
 *                        handle it from there).
 * @param map_CallLog_to_CALL_PHASE <u>[LinkedHashMap(Integer, String)]</u> --> A map with the TYPEs in CallLog.Call on its keys, and on its valus, the corresponding CALL_PHASEs.
 *                                     Example: <br><br>map_CallLog_to_CALL_PHASE.put(CallLog.Calls.INCOMING_TYPE, CALL_PHASE_ANSWERED);
 *                                                  <br>map_CallLog_to_CALL_PHASE.put(CallLog.Calls.MISSED_TYPE, CALL_PHASE_LOST).
 *
 * @return <u>[ String[][] ]</u> --> A double array of Strings in which each element contains the number and the phase call (CALL_PHASE) in which the number is currently in.
 *         There can be more than one event in a state change. It may be understood that a call had already been finished some time ago, or lost some time ago.
 *         Though, the events will always be in the actual event order. If a call was lost before another was answered, then the order will be exactly that one and not the opposite.
 */
public static String[][] get_call_phase(Context context, int state, String incomingNumber, ArrayList<ArrayList<String>> calls_state, LinkedHashMap<Integer, String> map_CallLog_to_CALL_PHASE) {
    switch (state) {
        case (CALL_STATE_RINGING): {
            //System.out.println("RINGING - " + incomingNumber);

            // New incoming call (there are no calls in the current processing call list).
            if (calls_state.size() == 0) { // Which means, was in IDLE.
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(incomingNumber);
                arrayList.add(BETTER_CALL_STATE_INCOMING);
                calls_state.add(arrayList);

                System.out.println(CALL_PHASE_RINGING_NEW + " -> " + incomingNumber);
                return new String[][]{new String[]{incomingNumber, CALL_PHASE_RINGING_NEW}};
            } else {
                // New incoming call waiting
                for (int i = 0; i < calls_state.size(); i++) {
                    if (calls_state.get(i).get(1).equals(BETTER_CALL_STATE_ACTIVE)) {
                        // If any call was already active and another one came, then that other one is waiting to be answered.
                        // This also works with 3 calls, even on case 8, since the state of the 1st call only changes on IDLE.
                        // Until then it remains ACTIVE, even having been already disconnected (don't know a way to detect it was disconnected).
                        ArrayList<String> arrayList = new ArrayList<>();
                        arrayList.add(incomingNumber);
                        arrayList.add(BETTER_CALL_STATE_WAITING);
                        calls_state.add(arrayList);

                        System.out.println(CALL_PHASE_RINGING_WAITING + " -> " + incomingNumber);
                        return new String[][]{new String[]{incomingNumber, CALL_PHASE_RINGING_WAITING}};
                        //break;
                    }
                }
            }

            break;
        }

        case (CALL_STATE_OFFHOOK): {
            //System.out.println("OFFHOOK - " + incomingNumber);
            String[] to_return = null;
            /*if (calls_state.size() == 0) {
                // If there are no calls in processing (for example, the app was started with at least one call already in course), abort and do nothing at all.
                // Can't have this here... Or it won't detect an outgoing call, which puts the phone in this state in the beginning.
                break;
            }*/

            // Check if it's an outgoing call.
            if (calls_state.size() == 0) { // Ou seja, estava em IDLE.
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(incomingNumber);
                arrayList.add(BETTER_CALL_STATE_OUTGOING);
                calls_state.add(arrayList);

                System.out.println(CALL_PHASE_OUTGOING + " -> " + incomingNumber);
                to_return = new String[]{incomingNumber, CALL_PHASE_OUTGOING};
            } else {
                // Check if the 1st or only call was answered.
                for (int i = 0; i < calls_state.size(); i++) {
                    if (PhoneNumberUtils.compareStrictly(calls_state.get(i).get(0), incomingNumber)) {
                        if (calls_state.get(i).get(1).equals(BETTER_CALL_STATE_INCOMING)) {
                            // If the number was in INCOMING (not WAITING, because I don't know how to detect a call waiting that is answered)
                            // and we are now in the OFFHOOK state, then the call was answered.
                            calls_state.get(i).set(1, BETTER_CALL_STATE_ACTIVE);

                            System.out.println(CALL_PHASE_ANSWERED + " -> " + incomingNumber);
                            return new String[][]{new String[]{incomingNumber, CALL_PHASE_ANSWERED}};
                        }
                    }
                }
            }

            // Add the number to the list with the state OFFHOOK, or update the state in case the number is already on the list.
            // This is for in case the cases above don't apply --> WAITING to OFFHOOK (don't know what to do with that - can't be rejected or answered).
            // Then in that case, I leave the state CALL_STATE_OFFHOOK on the list.
            for (int i = 0; i < calls_state.size(); i++) {
                if (PhoneNumberUtils.compareStrictly(calls_state.get(i).get(0), incomingNumber)) {
                    calls_state.get(i).set(1, String.valueOf(CALL_STATE_OFFHOOK));
                    break;
                }
            }
            return new String[][]{to_return};
            //break;
        }

        case (CALL_STATE_IDLE): {
            //System.out.println("IDLE - " + incomingNumber);
            ArrayList<String[]> final_return = new ArrayList<>();
            if (calls_state.size() == 0) {
                // If there are no calls in processing (for example, the app was started with at least one call already in course), abort and do nothing at all.
                break;
            }

            System.out.println("Aqui:");
            for (int i = 0; i < calls_state.size(); i++) {
                System.out.println(calls_state.get(i).get(0) + " | " + calls_state.get(i).get(1));
            }

            //////////////////////////////////////
            // Beginning of the LATE events

            // We begin by the LATE events for the correct order to go inthe return array of all events, the closes to reality possible.

            // Bellow is the handling of all numbers that didn't came with the state IDLE. Only one can come with the state and is the one for which the call was finished right now or lost right now.
            // The other would get no treatment. Therefore, this tried to understand what may have happened. All here will be of the LATE type because of exactly that (what happened --> past).

            // If it's more than a call, we can apply a "trick" to know the state of the first and of the last - answered or lost. This is an example of the cases 1 and 6 (2 calls) and 7 to 10 (3 calls).
            if (calls_state.size() > 1) {
                // If the first call would have been lost, this would have gone to IDLE directly, and the list would have gotten empty. Then the call coming next would
                // be the first call again. If there is a 2nd, the 1st must have been answered. And if it was answered, it was finished in some moment.

                // In case the 1st call wasn't the one that came in IDLE, then it was finished some time ago already.
                if (!calls_state.get(0).get(0).equals(incomingNumber)) {
                    // In 2 calls, if the 2nd comes on IDLE, then it means the 1st one was already finished a some time ago (because, again, if the 1st wasn't answered,
                    // there would be no 2nd). And this sets that state in the call and returns it.
                    // This can be applied for 3 or more calls too. In that case, if any call not the 1st gets on IDLE, the 1st was already finished some time ago.
                    System.out.println(CALL_PHASE_FINISHED_LATE + " -> " + calls_state.get(0).get(0));
                    final_return.add(new String[]{calls_state.get(0).get(0), CALL_PHASE_FINISHED_LATE});
                    calls_state.get(0).set(1, BETTER_CALL_STATE_DISCONNECTED);
                }
            }

            for (int i = 0; i < calls_state.size(); i++) {
                if (PhoneNumberUtils.compareStrictly(calls_state.get(i).get(0), incomingNumber)) {
                    if (!(calls_state.get(i).get(1).equals(BETTER_CALL_STATE_INCOMING) || calls_state.get(i).get(1).equals(BETTER_CALL_STATE_WAITING))) {
                        // In case the call didn't come from INCOMING or WAITING, then check if it was answered some time ago or not.
                        // Which means, if it wasn't detected the call was answered in the right moment (so it's not in ACTIVE state)...
                        if (!calls_state.get(i).get(1).equals(BETTER_CALL_STATE_ACTIVE)) {
                            System.out.println(CALL_PHASE_ANSWERED_LATE + " -> " + calls_state.get(i).get(0));
                            final_return.add(new String[]{calls_state.get(i).get(0), CALL_PHASE_ANSWERED_LATE}); // ... then it was answered some time ago already.
                        }
                    }
                    break;
                }
            }

            // For 3 or more calls, for all calls in the middle of the 1st and last, it's not possible to know their state without, at least, a way of knowing if any
            // ended in the middle or not. There, it would be possible to know that the 2nd one was answered, for example (in the case of 3 calls). But without that,
            // there's no way of knowing.
            // So, in that case, for the remaining calls, we're forced to go to the phone's call history.
            // This unless the case 9 happens. In that case, we can know the state of the 3 calls.
            // Sum up: this is done for all calls, except the 1st and last, and the one that got to IDLE. Supposing it's the 2nd in the case of 3 calls, nothing it's done.
            // In other cases, the calls that get here, we got get the state from the call history.
            // And this is done here to go in the correct order in the return array. After the handling of the 1st call and before the handling of the last call. And on the LATE events.
            if (calls_state.size() >= 3) {
                for (int i = 1; i < calls_state.size()-1; i++) {
                    if (calls_state.get(i).get(0).equals(incomingNumber)) {
                        continue;
                    }

                    int tipo_chamada = obter_tipo_ultima_chamada_numero(context, calls_state.get(i).get(0));
                    if (tipo_chamada == CallLog.Calls.INCOMING_TYPE || tipo_chamada == CallLog.Calls.MISSED_TYPE) {
                        System.out.println(map_CallLog_to_CALL_PHASE.get(tipo_chamada) + " -> " + calls_state.get(calls_state.size() - 1).get(0));
                        final_return.add(new String[]{calls_state.get(calls_state.size() - 1).get(0), map_CallLog_to_CALL_PHASE.get(tipo_chamada)});
                    }
                    calls_state.get(calls_state.size() - 1).set(1, BETTER_CALL_STATE_DISCONNECTED);
                }
                // TODO Imagine the same person calls, gives up, calls again, I hang up who I was talking with and answered this person.
                //  This will detect the state as the last one (answered), when I didn't answered the 1st time.
                // This should be in real-time detecting the exact states from the call history if it can't get them directly, but I don't have time to think on that.
                // In the app I'm making (an assistant), I don't need all the states when the happen exactly. Only missed calls in the end and inoming calls in the beginning.
            }

            if (calls_state.size() > 1 && !calls_state.get(calls_state.size() - 1).get(0).equals(incomingNumber)) {
                // The detection of the last call, in case it was lost some time ago, works both for 2 as for any other superior number of calls.
                // For only one call it's not necessary, because on that case, we know exactly when it's lost.

                // PS: The call that got to IDLE is never LOST_LATE - either lost right now or finished right now.

                // If the last call on the list got the OFFHOOK state (which means, without knowing if it was answered or lost in the right moment),
                // and didn't get here on IDLE, then by the cases 1 and 6 for 2 calls, and by the cases 7 to 10 for 3 calls, it wasn't answered either,
                // or it would have been that call getting to IDLE itself.
                // Getting the 1st one here on IDLE in the case of 2 calls or any other call in the case of 3 calls, then this last one was lost some time ago.
                if (calls_state.get(calls_state.size() - 1).get(1).equals(String.valueOf(CALL_STATE_OFFHOOK))) {
                    System.out.println(CALL_PHASE_LOST_LATE + " -> " + calls_state.get(calls_state.size() - 1).get(0));
                    final_return.add(new String[]{calls_state.get(calls_state.size() - 1).get(0), CALL_PHASE_LOST_LATE});
                    calls_state.get(calls_state.size() - 1).set(1, BETTER_CALL_STATE_DISCONNECTED);
                }
            }

            // End of LATE events
            //////////////////////////////////////

            // Now processing of the immediate events, so they get all in order (the late ones happened before the immediate ones).
            for (int i = 0; i < calls_state.size(); i++) {
                if (PhoneNumberUtils.compareStrictly(calls_state.get(i).get(0), incomingNumber)) {
                    if (calls_state.get(i).get(1).equals(BETTER_CALL_STATE_INCOMING) || calls_state.get(i).get(1).equals(BETTER_CALL_STATE_WAITING)) {
                        // If it came directly from INCOMING or WAITING states to IDLE, then the call was lost right now.
                        System.out.println(CALL_PHASE_LOST + " -> " + incomingNumber);
                        final_return.add(new String[]{incomingNumber, CALL_PHASE_LOST});
                    } else {
                        // If the state is not INCOMING or WAITING, this in case will be OFFHOOK or ANSWERED. Which means, the call was finished right now (means was alreaedy answered some ago - or would have been lost).
                        // In no case where a call goes from OFFHOOK to IDLE means the call was lost some time ago, from the testing. So the only option is the call having been finished, or lost, right now (which is handled on the above IF).
                        System.out.println(CALL_PHASE_FINISHED + " -> " + incomingNumber);
                        final_return.add(new String[]{incomingNumber, CALL_PHASE_FINISHED});
                    }
                    calls_state.get(i).set(1, BETTER_CALL_STATE_DISCONNECTED);
                    break;
                }
            }

            calls_state.clear();
            return final_return.toArray(new String[0][0]);
            //break;
        }
    }
    return null;
}

当电话状态更改时,我从telephonymanager.listen()方法内部调用此函数。我在一个类的构造函数中有一个方法,这个类示例化在一个永不停止的服务上,它是整个应用程序的主服务。基本上,这个对象总是在内存中。阅读那里的方法描述,知道如何处理它。希望我解释得很得体。如果我没有,请告诉我我能更好地解释什么。
如果有人有更好的解决方案,请随时分享!我会很感激的!一个比这个更好的方法,我看到的是在每一个电话状态的变化,去看看有没有变化,在通话记录,这取决于它的变化。但那会占用我更多的时间,我没有足够的时间。一个星期已经太多了,因为我不得不借我妈妈,爸爸和哥哥的电话,有时还得借家里的电话才能拿到这个哈哈哈。他们不是一直在家。
我将在这里留下github链接,了解完整理解代码所需的其他内容。例如,我在这里谈到了第x和y号案件。在这里:https://github.com/dadi590/detect-better-call-states-on-android,以及我曾经有过但没有工作的想法,或者我曾经有过但由于缺乏时间而无法实施的想法。

相关问题