com.nagopy.android.xposed.SettingChangedReceiver.java Source code

Java tutorial

Introduction

Here is the source code for com.nagopy.android.xposed.SettingChangedReceiver.java

Source

/*
 * Copyright (C) 2013 75py
 *
 * 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.
 */

package com.nagopy.android.xposed;

import java.lang.ref.WeakReference;

import org.apache.commons.lang3.StringUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.nagopy.android.xposed.utilities.util.Logger;

/**
 * ????????.
 */
public abstract class SettingChangedReceiver extends BroadcastReceiver {

    /**  */
    public final String action;

    /** ? */
    public final WeakReference<Object> dataObject;

    protected SettingChangedReceiver(Object dataObject, String action) {
        this.dataObject = new WeakReference<Object>(dataObject);
        this.action = action;
    }

    /**
     * ????????.
     */
    protected abstract void onDataChanged();

    @Override
    public void onReceive(Context context, Intent intent) {
        Object obj = dataObject.get();
        if (!StringUtils.equals(intent.getAction(), action) || obj == null) {
            context.unregisterReceiver(this);
            return;
        }

        Bundle extras = intent.getExtras();
        if (extras == null || extras.size() != 2) {
            return;
        }

        try {
            // target?value????????
            String target = extras.getString("target");
            Object value = extras.get("value");
            obj.getClass().getField(target).set(obj, value);

            onDataChanged();
        } catch (Throwable t) {
            Logger.e(getClass().getSimpleName(), Log.getStackTraceString(t));
        }
    }

    protected boolean isNotNull(Object... objects) {
        if (objects == null) {
            return false;
        }
        for (Object obj : objects) {
            if (obj == null) {
                return false;
            }
        }
        return true;
    }
}