Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012-2015 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.util.List;

public class Main {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    @NonNull
    private static String prepareCheckReceiverReport(@NonNull final Context context,
            @Nullable final String exceptionMessage, @Nullable final String receiverName,
            @NonNull final Intent broadcastIntent) {
        final StringBuilder reportBuilder = new StringBuilder("checkReceiver error").append(LINE_SEPARATOR)
                .append("Exception message : ").append(exceptionMessage).append(LINE_SEPARATOR)
                .append("Expected receiverName : ").append(receiverName).append(LINE_SEPARATOR)
                .append("Intent action : ").append(broadcastIntent.getAction());

        final PackageManager packageManager = context.getPackageManager();
        final List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(broadcastIntent,
                PackageManager.GET_INTENT_FILTERS);
        if (receivers == null) {
            reportBuilder.append(LINE_SEPARATOR).append("queryBroadcastReceivers returns null");
        } else if (receivers.isEmpty()) {
            reportBuilder.append(LINE_SEPARATOR).append("queryBroadcastReceivers returns empty list");
        } else {
            reportBuilder.append(LINE_SEPARATOR)
                    .append("queryBroadcastReceivers returns not empty list. List size : ").append(receivers.size())
                    .append(LINE_SEPARATOR).append("PackageName : ").append(context.getPackageName());

            int emptyPackageNameCounter = 0;
            int emptyReceiverNameCounter = 0;
            for (ResolveInfo receiver : receivers) {
                if (TextUtils.isEmpty(receiver.activityInfo.packageName)) {
                    ++emptyPackageNameCounter;
                } else if (receiver.activityInfo.packageName.equals(context.getPackageName())) {
                    reportBuilder.append(LINE_SEPARATOR).append("Receiver with right package : ")
                            .append(receiver.activityInfo.name);
                }
                if (TextUtils.isEmpty(receiver.activityInfo.name)) {
                    ++emptyReceiverNameCounter;
                } else if (receiver.activityInfo.name.equals(receiverName)) {
                    reportBuilder.append(LINE_SEPARATOR).append("Receiver with right name has package : ")
                            .append(receiver.activityInfo.packageName);
                }
            }

            reportBuilder.append(LINE_SEPARATOR).append("Receivers with empty package name : ")
                    .append(emptyPackageNameCounter).append(LINE_SEPARATOR).append("Receivers with empty name : ")
                    .append(emptyReceiverNameCounter);
        }

        return reportBuilder.toString();
    }
}