ca.rmen.android.networkmonitor.app.prefs.FilterColumnActivity.java Source code

Java tutorial

Introduction

Here is the source code for ca.rmen.android.networkmonitor.app.prefs.FilterColumnActivity.java

Source

/*
 * This source is part of the
 *      _____  ___   ____
 *  __ / / _ \/ _ | / __/___  _______ _
 * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/
 * \___/_/|_/_/ |_/_/ (_)___/_/  \_, /
 *                              /___/
 * repository.
 *
 * Copyright (C) 2014-2015 Carmen Alvarez (c@rmen.ca)
 *
 * 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.rmen.android.networkmonitor.app.prefs;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ca.rmen.android.networkmonitor.Constants;
import ca.rmen.android.networkmonitor.R;
import ca.rmen.android.networkmonitor.app.prefs.FilterColumnListFragment.FilterListItem;
import ca.rmen.android.networkmonitor.provider.NetMonColumns;
import ca.rmen.android.networkmonitor.util.Log;

/**
 * Activity which lets the user choose which values for a particular column will appear in the report.
 */
public class FilterColumnActivity extends AppCompatActivity { // NO_UCD (use default)
    private static final String TAG = Constants.TAG + FilterColumnActivity.class.getSimpleName();
    public static final String EXTRA_COLUMN_NAME = "column_name";
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter_columns);
        ListFragment lvf = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment);
        mListView = lvf.getListView();

        // Show a hint to the user, explaining what filtering this column does.
        TextView tvHint = (TextView) findViewById(R.id.filter_columns_hint);
        String columnName = getIntent().getStringExtra(EXTRA_COLUMN_NAME);
        String columnLabel = NetMonColumns.getColumnLabel(this, columnName);
        String hintText = getString(R.string.filter_columns_hint, columnLabel);
        tvHint.setText(hintText);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.filter_columns, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.action_select_all:
            for (int i = 0; i < mListView.getCount(); i++)
                mListView.setItemChecked(i, true);
            break;
        case R.id.action_select_none:
            for (int i = 0; i < mListView.getCount(); i++)
                mListView.setItemChecked(i, false);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    public void onCancel(@SuppressWarnings("UnusedParameters") View v) {
        Log.v(TAG, "onCancel");
        finish();
    }

    public void onOk(@SuppressWarnings("UnusedParameters") View v) {
        Log.v(TAG, "onOk");
        // Update the preference for values to filter, for this particular column.

        // Build a list of all the values the user selected.
        SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions();
        final List<String> selectedValues = new ArrayList<>(mListView.getCount());
        for (int i = 0; i < mListView.getCount(); i++) {
            if (checkedPositions.get(i))
                selectedValues.add(((FilterListItem) mListView.getAdapter().getItem(i)).value);
        }
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                // Update the filter preference for this column.
                String columnName = getIntent().getExtras().getString(EXTRA_COLUMN_NAME);
                NetMonPreferences.getInstance(FilterColumnActivity.this).setColumnFilterValues(columnName,
                        selectedValues);
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        }.execute();
    }
}