ru.lizaalert.common.ui.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for ru.lizaalert.common.ui.MainActivity.java

Source

/*
Copyright (c) 2014 Denis Volyntsev <fortun777@gmail.com>
Copyright (c) 2014 Other contributors as noted in the AUTHORS file.
    
  ???? ? ? "  ? 
?    ?".
    
? ?  ,   " 
?  ?    ?"  ??
 (    ?),
 ?  ?  , ?
   ?, , , ,
, ??, ? /  
 ??,    ,  ???? 
 ?,  ? ? ?:
    
    ?    ??  
  ?    ?   ??.
    
??? ?? ? ? ? ,  ?-
???, ? ???  ??, ?, ? ?
??? ??? ?? ?,   
?? ?????   ??? ?. ?  ? ?
?  ??? ? ? ??  ?  ?
?,    ??   ???, ?
 ?, ? ,  ?  ???  ??
?  ?? ?? ?  ?
  ?? ?.
    
 ?  ? ,   "?  
 ?",   " ? ? "
?",     ?    
?     ??,   ,
?     ?  ? 
? ? ?.
    
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
    
Except as contained in this notice, the name of Liza Alert or the name of
Liza Alerts's hotline department or the name(s) the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
 */

package ru.lizaalert.common.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.SearchView;

import com.yandex.metrica.YandexMetrica;
import ru.lizaalert.common.R;
import ru.lizaalert.hotline.lib.settings.Settings;
import ru.lizaalert.hotline.lib.yp.MySuggestionProvider;
import ru.lizaalert.hotline.lib.yp.YellowPagesLoader;
import ru.lizaalert.hotline.lib.yp.ui.YellowPagesFragment;

public class MainActivity extends Activity implements ActionBar.TabListener {

    public static final String TAG = "8800";

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        assert actionBar != null;
        actionBar.setDisplayShowTitleEnabled(false);
        //   ?   , ? ?   ,  ? 
        //        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager());

        Log.d("8800", "mViewPager " + mViewPager);
        Log.d("8800", "mSectionsPagerAdapter " + mSectionsPagerAdapter);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
                if (position == 1) {
                    if (getCurrentFocus() != null) {
                        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                                INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    }
                }

            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

            actionBar
                    .addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

        if (!Settings.instance(this).isLicenceAccepted()) {
            LicenceDialog licenceDialog = new LicenceDialog();
            licenceDialog.setListener(new LicenceDialog.Listener() {
                @Override
                public void onAcceptLicence(DialogInterface dialog) {
                    dialog.dismiss();
                    Settings.instance(MainActivity.this).setLicenceAccepted(true);
                }

                @Override
                public void onDeclineLicence(DialogInterface dialog) {
                    dialog.dismiss();
                    finish();
                }
            });
            licenceDialog.show(this);

        }
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private final String[] titles;

        public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            titles = context.getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                //                    return new InputFormFragment();
                //                case 1:
                return new YellowPagesFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.yp_menu, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) item.getActionView();

        // Assumes current activity is the searchable activity
        assert searchView != null;
        assert searchManager != null;
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        //        searchView.setIconifiedByDefault(false);

        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        YandexMetrica.onResumeActivity(this);
    }

    @Override
    protected void onPause() {
        YandexMetrica.onPauseActivity(this);
        super.onPause();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
        case R.id.action_clear_history:
            SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, MySuggestionProvider.AUTHORITY,
                    MySuggestionProvider.MODE);
            suggestions.clearHistory();
            return true;
        case R.id.action_search:
            onSearchRequested();
            return true;
        case R.id.action_about:
            startActivity(new Intent(this, About.class));
            return true;
        case R.id.action_update_yp:
            YellowPagesLoader.getInstance(this).fetchDataAsync();
            return true;
        }
        return (super.onOptionsItemSelected(menuItem));
    }
}