ca.farrelltonsolar.classic.MessageFragment.java Source code

Java tutorial

Introduction

Here is the source code for ca.farrelltonsolar.classic.MessageFragment.java

Source

/*
 * Copyright (c) 2014. FarrelltonSolar
 *
 *  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 ca.farrelltonsolar.classic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.ListFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Graham on 29/12/2014.
 */
public class MessageFragment extends ListFragment {

    private boolean isReceiverRegistered;
    protected ViewGroup container;
    MessageListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        adapter = new MessageListAdapter(container.getContext());
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (isReceiverRegistered) {
            try {
                LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(mReadingsReceiver);
            } catch (IllegalArgumentException e) {
                // Do nothing
            }
            isReceiverRegistered = false;
        }
        Log.d(getClass().getName(), "onStop");
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!isReceiverRegistered) {
            LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mReadingsReceiver,
                    new IntentFilter(Constants.CA_FARRELLTONSOLAR_CLASSIC_READINGS));
            isReceiverRegistered = true;
        }
        Log.d(getClass().getName(), "onStart");
    }

    // Our handler for received Intents.
    protected BroadcastReceiver mReadingsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle readings = intent.getBundleExtra("readings");
            int infoFlag = readings.getInt(RegisterName.InfoFlagsBits.name(), 0);
            adapter.clear();
            int bitMask = 0x40000000;
            while (bitMask > 0) { // until all bits are zero
                if ((bitMask & infoFlag) != 0) {
                    Pair<Severity, String> item = MonitorApplication.getMessage(bitMask & infoFlag);
                    if (item != null) {
                        adapter.add(item);
                    }
                }
                bitMask >>= 1; // shift bits, removing lower bit
            }
            adapter.notifyDataSetChanged();
        }
    };

}