com.xxjwd.sjbg.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.xxjwd.sjbg.MainActivity.java

Source

/*
* Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * 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 com.xxjwd.sjbg;

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

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xxjwd.adapter.LeftMenuAdapter;
import com.xxjwd.adapter.MyPagerAdapter;
import com.xxjwd.chat.RecorderVideoActivity;
import com.xxjwd.classes.LeftMenuItem;
import com.xxjwd.libs.AnimFragmentActivity;
import com.xxjwd.libs.MyApp;
import com.xxjwd.libs.PagerSlidingTabStrip;
import com.xxjwd.transfer.Transfer;
import com.xxjwd.util.FileUtil;
import com.xxjwd.sjbg.R;


public class MainActivity extends AnimFragmentActivity {

   private final Handler handler = new Handler();

   private PagerSlidingTabStrip tabs;
   private ViewPager pager;
   private MyPagerAdapter adapter;
   private MyApp app;
   private Drawable oldBackground = null;
   private int currentColor = 0xFF3F9FE0;

   private DrawerLayout mDrawerLayout;
   private ListView mDrawerList;
   private View headview;
   private LeftMenuAdapter mAdapter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
      pager = (ViewPager) findViewById(R.id.pager);
      app = (MyApp)getApplication();

      //if (! Connection.getInstance(this).isConnectedOrConnecting())
      //{
         //Connection.getInstance(this).connect();
      //}
      adapter = new MyPagerAdapter(getSupportFragmentManager(),app.getMenu());

      pager.setAdapter(adapter);

      final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
      pager.setPageMargin(pageMargin);

      tabs.setViewPager(pager);

      changeColor(currentColor);
      getActionBar().setDisplayHomeAsUpEnabled(false);
      if (Transfer.isTesting())
      {
         getActionBar().setTitle("");
      }
      else
      {
         getActionBar().setTitle("");
      }
      setLeftMenu();
      
   }
   
   
   private void setLeftMenu()
   {
      //menu
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            headview = LayoutInflater.from(this).inflate(R.layout.leftmenu_loginuser, null);
            TextView gh = (TextView)headview. findViewById(R.id.leftmneu_login_gh);
            TextView xm =  (TextView)headview.findViewById(R.id.leftmenu_login_name);
            gh.setText(app.getUser().getUserNo());
            xm.setText(app.getUser().getUserName());
            mDrawerList.addHeaderView(headview);

            List<LeftMenuItem> list = new ArrayList<LeftMenuItem>();
            list.add(new LeftMenuItem(0, "", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            list.add(new LeftMenuItem(0, "", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            //list.add(new LeftMenuItem(1, "",0));
            list.add(new LeftMenuItem(0, "?", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            list.add(new LeftMenuItem(1, "????", 0));
            list.add(new LeftMenuItem(1, "?", 0));
            mAdapter=new LeftMenuAdapter(list, this);
            mDrawerList.setAdapter(mAdapter);
            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
            //mDrawerLayout.setDrawerShadow(R.drawable.slidingmenu_shadow, GravityCompat.END);
   }
   /* The click listner for ListView in the navigation drawer */
   private class DrawerItemClickListener implements
         ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
         if (view.getId() == headview.getId()) return;
         if (mAdapter.getItem(position-1).getType() == 1)
         {
            mDrawerLayout.closeDrawers();
            doDrawerItemClick(mAdapter.getItem(position-1).getTitle());
         }
         //Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
         
      }

   }

   private void doDrawerItemClick(String title)
   {
      switch(title)
      {
         case "?":
            doLogout();
            break;
         case "?":
            doClearCache();
            break;
         case "?":
            doShowDownloadDialog();
            break;
         case "????":
            doFeedback();
            break;
         case "?":
            doShowLoginRecord();
            break;
         case "?":
            doAbout();
            break;
         case "?":
            doSystemMessage();
            break;
         default:
            break;
      }
   }
   
   private void doSystemMessage() {
      // TODO Auto-generated method stub
      Intent intent=new Intent();
        intent.setClass(MainActivity.this,MessageListActivity.class);
        startActivity(intent);
   }


   private void doAbout() {
      Intent intent=new Intent();
        intent.setClass(MainActivity.this, AboutActivity.class);
        startActivity(intent);
   }


   private void doShowLoginRecord() {
      Intent intent=new Intent();
        //intent.setClass(MainActivity.this, LoginRecordListActivity.class);
      intent.setClass(MainActivity.this, RecorderVideoActivity.class);
        startActivity(intent);
   }


   private void doFeedback() {
      Intent intent=new Intent();
        intent.setClass(MainActivity.this, FeedbackActivity.class);
        startActivity(intent);
   }


   private void doLogout() {
      // TODO Auto-generated method stub

      new AlertDialog.Builder(this,R.style.dialog).setTitle("??").setMessage("??????")
      .setPositiveButton("", new DialogInterface.OnClickListener() {
 
          @Override
          public void onClick(DialogInterface dialog, int which) {
         // ???
             //Connection.getInstance(MainActivity.this).disconnect();
            app.setUser(null);
            app.setMenu(null);
            
            finish();
          }
      })
      .setNegativeButton("?", new DialogInterface.OnClickListener() {
 
          @Override
          public void onClick(DialogInterface dialog, int which) {
         // ???,??
          }
      }).show();
   }


   private void doClearCache() {
      // TODO Auto-generated method stub
      new AlertDialog.Builder(this,R.style.dialog).setTitle("?").setMessage("??.")
      .setPositiveButton("", new DialogInterface.OnClickListener() {
 
          @Override
          public void onClick(DialogInterface dialog, int which) {
         // ???
             FileUtil.ClearCache();
             Toast.makeText(MainActivity.this, "?", Toast.LENGTH_SHORT).show();
             
          }
      })
      .setNegativeButton("?", new DialogInterface.OnClickListener() {
 
          @Override
          public void onClick(DialogInterface dialog, int which) {
         // ???,??
          }
      }).show();
   // super.onBackPressed();
      
   }


   private void doShowDownloadDialog() {
      Dialog builder = new Dialog(this);
       //builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
      builder.setTitle("???");
       builder.getWindow().setBackgroundDrawable(
           new ColorDrawable(android.graphics.Color.TRANSPARENT));
       builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
           @Override
           public void onDismiss(DialogInterface dialogInterface) {
               //nothing;
           }
       });

       ImageView imageView = new ImageView(this);
       imageView.setImageResource(R.drawable.erweima);
      imageView.setOnLongClickListener(new OnLongClickListener()
      {

         @Override
         public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();        
              intent.setAction("android.intent.action.VIEW");    
              Uri content_url = Uri.parse("http://61.163.45.215:808/sjbg/download.html");   
              intent.setData(content_url);  
              startActivity(intent);
            return true;
         }
      }
      );
       builder.addContentView(imageView, new RelativeLayout.LayoutParams(
               ViewGroup.LayoutParams.WRAP_CONTENT, 
               ViewGroup.LayoutParams.WRAP_CONTENT));
       builder.show();
      
   }



   
    @Override
    protected void onNewIntent(Intent intent) {
        String action = intent.getAction();


        Toast.makeText(this, "NewIntent:" +  action , Toast.LENGTH_SHORT).show();
    }

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

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {


      return super.onOptionsItemSelected(item);
   }

   private void changeColor(int newColor) {

      tabs.setIndicatorColor(newColor);

      // change ActionBar color just if an ActionBar is available
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

         Drawable colorDrawable = new ColorDrawable(newColor);
         Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
         LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });
         
         if (oldBackground == null) {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
               ld.setCallback(drawableCallback);
            } else {
               getActionBar().setBackgroundDrawable(ld);
            }

         } else {

            TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });

            // workaround for broken ActionBarContainer drawable handling on
            // pre-API 17 builds
            // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
               td.setCallback(drawableCallback);
            } else {
               getActionBar().setBackgroundDrawable(td);
            }

            td.startTransition(200);

         }

         oldBackground = ld;

         // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
         getActionBar().setDisplayShowTitleEnabled(false);
         getActionBar().setDisplayShowTitleEnabled(true);

      }

      currentColor = newColor;

   }

   public void onColorClicked(View v) {

      int color = Color.parseColor(v.getTag().toString());
      changeColor(color);

   }

   @Override
   protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt("currentColor", currentColor);
   }

   @Override
   protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      currentColor = savedInstanceState.getInt("currentColor");
      changeColor(currentColor);
   }

   private Drawable.Callback drawableCallback = new Drawable.Callback() {
      @Override
      public void invalidateDrawable(Drawable who) {
         getActionBar().setBackgroundDrawable(who);
      }

      @Override
      public void scheduleDrawable(Drawable who, Runnable what, long when) {
         handler.postAtTime(what, when);
      }

      @Override
      public void unscheduleDrawable(Drawable who, Runnable what) {
         handler.removeCallbacks(what);
      }
   };

    



}