edu.utexas.cs.bevomaps.MapDrawerVC.java Source code

Java tutorial

Introduction

Here is the source code for edu.utexas.cs.bevomaps.MapDrawerVC.java

Source

/**
 *
 * Copyright  2015 Bevo Maps
 *
 * 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 edu.utexas.cs.bevomaps;

import android.app.Activity;
import android.os.Build;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.ViewGroup;

class MapDrawerVC implements View.OnClickListener {

    // Fields----------------------------------------------------------------------------------------

    private final DrawerLayout drawerLayout;
    private final View aboutButton, satelliteButton;

    private OnDrawerEventListener eventListener;

    // Constructors----------------------------------------------------------------------------------

    MapDrawerVC(Activity activity) {
        if (activity == null) {
            throw new IllegalArgumentException("Activity may not be null");
        }

        drawerLayout = (DrawerLayout) activity.findViewById(R.id.map_drawer);
        aboutButton = activity.findViewById(R.id.drawer_about);
        satelliteButton = activity.findViewById(R.id.drawer_satellite);

        if (drawerLayout == null || aboutButton == null || satelliteButton == null) {
            throw new IllegalStateException("Could not find all subviews");
        }

        displaceIfNeeded(activity);
        aboutButton.setOnClickListener(this);
        satelliteButton.setOnClickListener(this);
    }

    // Methods---------------------------------------------------------------------------------------

    @Override
    public void onClick(View view) {
        if (eventListener != null) {
            if (view == aboutButton) {
                eventListener.onAboutClicked();
            } else if (view == satelliteButton) {
                eventListener.onSatelliteClicked();
            }
        }
    }

    void openDrawer() {
        drawerLayout.openDrawer(GravityCompat.START);
    }

    void closeDrawer() {
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    boolean isSatelliteSelected() {
        return satelliteButton.isSelected();
    }

    void setSatelliteSelected(boolean selected) {
        satelliteButton.setSelected(selected);
    }

    private void displaceIfNeeded(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int id = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (id > 0) {
                View searchBox = activity.findViewById(R.id.drawer_header);
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) searchBox.getLayoutParams();

                params.topMargin = activity.getResources().getDimensionPixelSize(id);
                searchBox.setLayoutParams(params);
            }
        }
    }

    void setOnDrawerEventListener(OnDrawerEventListener listener) {
        eventListener = listener;
    }

    // Interfaces------------------------------------------------------------------------------------

    interface OnDrawerEventListener {
        void onAboutClicked();

        void onSatelliteClicked();
    }
}