package com.novelbook.android; import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.AppCompatSpinner; import android.util.DisplayMetrics; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.novelbook.android.AD.toutiao.TTAdManagerHolder; import com.novelbook.android.Fragments.BasicFragment; import com.novelbook.android.Fragments.Fragment_Shelf; import com.novelbook.android.Fragments.Fragment_bookStore; import com.novelbook.android.Fragments.Fragment_booklist; import com.novelbook.android.Fragments.Fragment_paihang; import com.novelbook.android.netsubscribe.BookSubscribe; import com.novelbook.android.netutils.NetUtil; import com.novelbook.android.netutils.OnSuccessAndFaultListener; import com.novelbook.android.netutils.OnSuccessAndFaultSub; import com.novelbook.android.service.ServiceDownload; import com.novelbook.android.utils.CommonUtil; import com.novelbook.android.utils.Config; import com.novelbook.android.utils.Constants; import com.novelbook.android.utils.GsonUtil; import com.novelbook.android.utils.PageFactory; import org.json.JSONObject; import org.litepal.util.Const; import java.util.Date; import butterknife.BindView; import butterknife.OnClick; import static com.novelbook.android.FileActivity.EXTERNAL_STORAGE_REQ_CODE; public class Main2Activity extends Activity_base implements NavigationView.OnNavigationItemSelectedListener{//} , BasicFragment.OnFragmentInteractionListener { // public MyApp app ; public static final String TAG =Main2Activity.class.getSimpleName(); // @BindView(R.id.fab) // FloatingActionButton fab ; @BindView(R.id.nav_view ) NavigationView navigationView; @BindView(R.id.navigation ) BottomNavigationView botoomNavigation; @BindView(R.id.drawer_layout) DrawerLayout drawerLayout; @BindView(R.id.rgSex) RadioGroup rgSex; // @BindView(R.id.tvUser) // TextView tvUser; private Config config; @Override public int getLayoutRes() { return R.layout.activity_main2; } @Override protected void initViews() { } public static volatile Activity activityContext = null; @Override protected void onCreate(Bundle savedInstanceState) { NetUtil.getHostPolicy(); super.onCreate(savedInstanceState); activityContext =this; //PageFactory.createPageFactory(this); // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); config =Config.getInstance(); // ButterKnife.bind(this); initialSexOption(); //checkUpdate(true); CommonUtil.getSearchTabTtitle(this); DisplayMetrics metric = new DisplayMetrics(); WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metric); Constants.SCREEN_WIDTH_PIX = metric.widthPixels; // mHeight = metric.heightPixels; // mHeight = CommonUtil.getDpi(context); Constants.SCREEN_HEIGHT_PIX = CommonUtil.getScreenHeight(this) - CommonUtil.statusBarDiff(this); } void initialSexOption(){ Constants.SEX = config.getSexOption(); rgSex.clearCheck(); // ((RadioButton) rgSex.getChildAt(0)).setChecked(Constants.SEX ==1); // ((RadioButton) rgSex.getChildAt(1)).setChecked(Constants.SEX ==2); ((RadioButton) rgSex.getChildAt(Constants.SEX-1)).setChecked(true); Log.d(TAG, "onCreate: sexoption is" + Constants.SEX); } @Override protected void setTitle() { setTitle(""); } @Override protected void initData() { creatToolbar(); setupNavi(); showHome(); // tvUser.setText(this.getPackageName()); } String currentActiveFragment; private void showFragment(Fragment fragment, String fragmentTag) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (findViewById(R.id.realtabcontent) != null) { //realtabcontent ft.replace(R.id.realtabcontent, fragment,fragmentTag); ft.addToBackStack(null); //add to backstack. } else { try{ ft.add(R.id.realtabcontent, fragment,fragmentTag); }catch(Exception e){ // Utils.logStackTrace(e, dTAG); } } currentActiveFragment =fragmentTag; // ft.setTransition(2500);//TRANSIT_FRAGMENT_OPEN ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commitAllowingStateLoss(); } private ActionBarDrawerToggle toggle; private void creatToolbar() { // toolbar= (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(false); // toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);//设置导航栏图标 // toolbar.setLogo(R.mipmap.ic_launcher);//设置app logo // toolbar.setTitle("Title");//设置主标题 // setTitle("titleaa"); // toolbar.setSubtitle("Subtitle");//设置子标题 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int menuItemId = item.getItemId(); /* if (menuItemId == R.id.menuFenlei) { Intent intent = new Intent(Main2Activity.this, activity_cates.class); startActivity(intent); }else */ if(menuItemId == R.id.menuMore) { showShelfMoreSheet(); } else if(menuItemId == R.id.menuFinshi) { udateShelfZhengli(); } else if(menuItemId==R.id.menuSearch){ Intent intent = new Intent(Main2Activity.this,Activity_Search.class); startActivity(intent); }else if(menuItemId==R.id.menuHistory){ Intent intent = new Intent(Main2Activity.this,Activity_paihangbang.class); intent.putExtra(Activity_paihangbang.EXTR_HISTORY,"yes"); intent.putExtra(Activity_paihangbang.EXTR_TITLE,"我曾经看过的书"); startActivity(intent); } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void udateShelfZhengli() { switchShelfZhengli(true); } Fragment_Shelf fragment_shelf; public void setShelfFragment(Fragment_Shelf fragment_shelf){ this.fragment_shelf=fragment_shelf; } private void showShelfMoreSheet() { { if(fragment_shelf!=null) fragment_shelf.showShelfMoreSheet(); } } void setupNavi() { //drawer navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); // TextView tvuser= (TextView) navigationView. findViewById(R.id.tvUser); // tvuser.setText(this.getPackageName()); // mTextMessage = (TextView) findViewById(R.id.message); botoomNavigation = (BottomNavigationView) findViewById(R.id.navigation); botoomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_history) { Intent intent = new Intent(Main2Activity.this,Activity_paihangbang.class); intent.putExtra(Activity_paihangbang.EXTR_HISTORY,"yes"); intent.putExtra(Activity_paihangbang.EXTR_TITLE,"我曾经看过的书"); startActivity(intent); } else if (id == R.id.nav_huancun) { startDownloadService(); Intent intent = new Intent(Main2Activity.this,Activity_cache.class); startActivity(intent); } else if (id == R.id.nav_advice) { Uri uri = Uri.parse("mailto:"+ Constants.email); String[] email = {Constants.email}; Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra(Intent.EXTRA_CC, email); // 抄送人 intent.putExtra(Intent.EXTRA_SUBJECT, "意见建议"); // 主题 intent.putExtra(Intent.EXTRA_TEXT, "请输入内容"); // 正文 startActivity(Intent.createChooser(intent, "请选择邮件应用")); } else if (id == R.id.nav_setup) { Intent intent = new Intent(Main2Activity.this,ActivitySetup.class); startActivity(intent); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } private void startDownloadService() { Intent serviceIntent = new Intent(this, ServiceDownload.class); startService(serviceIntent); } private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: //mTextMessage.setText(R.string.title_home); bottomSelectedIndex =0; showHome(); return true; case R.id.navigation_dashboard: // mTextMessage.setText(R.string.title_dashboard); bottomSelectedIndex=1; showBookStore(); return true; case R.id.navigation_notifications: bottomSelectedIndex=2; // mTextMessage.setText(R.string.title_notifications); showPaihang(); return true; } return false; } }; //@BindView(R.id.sexSelect) //AppCompatSpinner spinner; @BindView(R.id.toolbarTitle) TextView txtTitle; private int bottomSelectedIndex; public void swithToolbarItems( ) { Log.e( TAG, "swithToolbarItems: " ); boolean showToggle =true;//bottomSelectedIndex !=1; boolean showSpinner = bottomSelectedIndex ==1; boolean showTitle = bottomSelectedIndex !=1; String title = bottomSelectedIndex==0?"我的书架":"排行榜"; // txtTitle.setVisibility(View.GONE); toggle.setDrawerIndicatorEnabled(showToggle); toggle.syncState(); //spinner.setVisibility( showSpinner?View.VISIBLE:View.GONE); rgSex.setVisibility( showSpinner?View.VISIBLE:View.GONE); supportInvalidateOptionsMenu(); //invalidateOptionsMenu(); if(bottomSelectedIndex==1) { title =""; } txtTitle.setText(title); // this.setTitle(title); } @Override public boolean onPrepareOptionsMenu(Menu menu) { Log.e(TAG, "onPrepareOptionsMenu"); menu.findItem(R.id.menuFinshi).setVisible(false); // menu.findItem(R.id.menuSearch).setVisible(true); if(bottomSelectedIndex==0) { menu.findItem(R.id.menuFinshi).setVisible(isShelfZhengli); if(isShelfZhengli){ // menu.findItem(R.id.menuFenlei).setVisible(false); menu.findItem(R.id.menuMore).setVisible(false); menu.findItem(R.id.menuSearch).setVisible(false); menu.findItem(R.id.menuHistory).setVisible(false); }else { // menu.findItem(R.id.menuFenlei).setVisible(false); menu.findItem(R.id.menuMore).setVisible(true); } }else if(bottomSelectedIndex==1) { // menu.findItem(R.id.menuFenlei).setVisible(true); menu.findItem(R.id.menuMore).setVisible(false); }else if(bottomSelectedIndex==2) { // menu.findItem(R.id.menuFenlei).setVisible(false); menu.findItem(R.id.menuMore).setVisible(false); } return super.onPrepareOptionsMenu(menu); } private void showHome() { Fragment_Shelf fragment_Shelf = (Fragment_Shelf) getSupportFragmentManager() .findFragmentByTag(Fragment_Shelf.TAG ); if(fragment_Shelf==null) fragment_Shelf =new Fragment_Shelf(); showFragment(fragment_Shelf,Fragment_Shelf.TAG); swithToolbarItems(); } private void showBookStore() { Fragment_bookStore fragment = (Fragment_bookStore) getSupportFragmentManager() .findFragmentByTag(Fragment_bookStore.TAG); if(fragment ==null) fragment =new Fragment_bookStore(); showFragment(fragment , Fragment_bookStore.TAG); swithToolbarItems(); } private void showOther() { } private void showPaihang(){ Fragment_paihang fragment = (Fragment_paihang) getSupportFragmentManager() .findFragmentByTag(Fragment_paihang.TAG); if(fragment ==null) fragment =new Fragment_paihang(); showFragment(fragment ,Fragment_paihang.TAG); swithToolbarItems( ); } long firstTime =0; @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (drawerLayout.isDrawerOpen(navigationView)) { drawerLayout.closeDrawers(); return true; } int cnt=0; long secondTime = System.currentTimeMillis(); if (secondTime - firstTime > 800) {//如果两次按键时间间隔大于800毫秒,则不退出 String msg= "再按一次将退出" ; Toast.makeText( this, msg, Toast.LENGTH_SHORT).show(); firstTime = secondTime;//更新firstTime return true; } else { // return super.onKeyUp(keyCode, event); // return super.onKeyUp(keyCode, event); // return false; finish(); application.exit(); } } try{ return super.onKeyUp(keyCode, event); } catch(Exception e){ Log.d("mainActivity","onkeyup出错" ); return true; } } @Override public void onResume() { super.onResume(); // swithToolbarItems( ); // setTitle("title on resume"); // checkUpdate(true); // getHostPolicy(); Fragment current = getSupportFragmentManager().findFragmentById(R.id.realtabcontent); if (current != null && current instanceof Fragment_Shelf){ // Log.d(TAG, "onResume: current is shelf, botoomNavigation selection is 0? " +(botoomNavigation.getSelectedItemId() == R.id.navigation_home)); if(botoomNavigation.getSelectedItemId()!= R.id.navigation_home){ botoomNavigation.setSelectedItemId( R.id.navigation_home); // Log.d(TAG, "onResume: set botoomNav selection to 0"); } } } @Override public void fillData() { } private void setSexOption(){ int s = rgSex.getCheckedRadioButtonId() == R.id.radioButtonBoy ?1:2; if(s!= Constants.SEX){ TTAdManagerHolder.initOnSexChange(MyApp.applicationContext); Constants.SEX=s; config.setSexOption(s); Fragment current = getSupportFragmentManager().findFragmentById(R.id.realtabcontent); if (current != null && current instanceof BasicFragment){ ((Fragment_bookStore)current).onSexSwitch(); } } } @OnClick({R.id.radioButtonBoy,R.id.radioButtonGirl}) void Onclick(View view){ switch (view.getId()) { case R.id.radioButtonBoy: setSexOption(); break; case R.id.radioButtonGirl: setSexOption(); break; } } public void hideBottomNavi(){ botoomNavigation.setVisibility(View.GONE); } boolean isShelfZhengli =false; public void switchShelfZhengli(boolean finished){ if(finished) { isShelfZhengli = false; swithToolbarItems(); botoomNavigation.setVisibility(View.VISIBLE); fragment_shelf.initReceyleView(); toggle.setDrawerIndicatorEnabled(true); toggle.syncState(); }else { toggle.setDrawerIndicatorEnabled(false); toggle.syncState(); supportInvalidateOptionsMenu(); isShelfZhengli = true; botoomNavigation.setVisibility(View.GONE); } } //----get master domain }