package com.jiuqi.njt.ui; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.json.JSONArray; import org.json.JSONObject; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.jiuqi.mobile.nigo.comeclose.bean.base.RoleCode; import com.jiuqi.njt.R; import com.jiuqi.njt.data.CheckStateInterface; import com.jiuqi.njt.data.MyApp; import com.jiuqi.njt.data.OptsharepreInterface; import com.jiuqi.njt.data.WorkTaskBeanLocal; import com.jiuqi.njt.db.WorkTaskNewsBeanDBHelper; import com.jiuqi.njt.register.IndexActivityViewBean; import com.jiuqi.njt.register.IndexActivityViewBean.LayoutType; import com.jiuqi.njt.register.IndexActivityViewBean.ViewType; import com.jiuqi.njt.register.ModuleOnClick; import com.jiuqi.njt.register.RegisterUtils; import com.jiuqi.njt.service.UpdateService; import com.jiuqi.njt.update.Config; import com.jiuqi.njt.update.NetworkTool; import com.jiuqi.njt.util.Constants; import com.jiuqi.njt.util.UIUtil; import com.jiuqi.njt.widget.NjtTitleBar; import com.jiuqi.njt.widget.SlidingMenu; /** * 农机通大众版首页 * * @author joe * */ public class IndexMainActivity extends BaseActivity implements OnClickListener{ private Context context = this; private String TAG = this.getClass().getName(); private MyApp application; private OptsharepreInterface sharePre; /** * 中间布局容器 */ private LinearLayout nullActivity_middleLinearLayout; /** * 左布局容器 */ private LinearLayout nullActivity_leftLinearLayout; /** * 右布局容器 */ private LinearLayout nullActivity_rightLinearLayout; /** * 左半边布局的数据对象集合 */ private List<IndexActivityViewBean> leftDataList; /** * 右半边布局的数据对象集合 */ private List<IndexActivityViewBean> rightDataList; /** * 记录滚动园点的索引 */ private AtomicInteger atomicInteger = new AtomicInteger(0); private ViewPager adViewPager; private ArrayList<View> pageViews; private ImageView[] imageViews; private ImageView imageView; private AdPageAdapter adapter; private boolean isContinue = true; private RelativeLayout rlUpdateNotice; private TextView tvUpdateTitle; private ImageView ivClose; private LoginSuccessReceiver loginReceiver; private Button leftbtn ; private Button rightBtn; private WorkTaskNewsBeanDBHelper dbhelper; private int type; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); doinit(); } private void doinit() { initParam(); initWidgets(); initUI(); initListeners(); checkUpdate();// 版本更新检查 } private void initParam() { application = (MyApp) getApplication(); sharePre = new OptsharepreInterface(context); leftDataList = new ArrayList<IndexActivityViewBean>(); leftDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_TOP_TEXT_RIGHAT_BOTTOM_IMAGE, LayoutType.LEFTNOMALBUTTON, R.drawable.index_1_zhaonh_bg, new ModuleOnClick(context, 5), R.drawable.index_1_zhaonh_new, "找农活", R.color.white)); leftDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_MIDLE_IMAGE_RIGHT_CETER_TEXT, LayoutType.LEFTNOMALBUTTON, R.drawable.index_3_hezs_bg, new ModuleOnClick(context, 41), R.drawable.index_3_hezs_new1, "合作社", R.color.white)); leftDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_TOP_TEXT_RIGHAT_BOTTOM_IMAGE, LayoutType.LEFTNOMALBUTTON, R.drawable.index_5_weixby_bg, new ModuleOnClick(context, 25), R.drawable.index_5_weixby_new, "维修保养", R.color.white)); leftDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_MIDLE_IMAGE_RIGHT_CETER_TEXT, LayoutType.LEFTBOTTPMBUTTON, R.drawable.index_7_fuwjg_bg, new ModuleOnClick(context, 88), R.drawable.index_7_fuwjgg_new, "服务机构", R.color.white)); rightDataList = new ArrayList<IndexActivityViewBean>(); rightDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_TOP_TEXT_RIGHAT_BOTTOM_IMAGE, LayoutType.RIGHTNOMALBUTTON, R.drawable.index_2_zhaojs_bg, new ModuleOnClick(context, 43), R.drawable.index_2_zhaojs_new, "找机手", R.color.white)); rightDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_TOP_TEXT_RIGHAT_BOTTOM_IMAGE, LayoutType.RIGHTNOMALBUTTON, R.drawable.index_4_chazb_bg, new ModuleOnClick(context, 48), R.drawable.index_4_chazb_new, "查周边", R.color.white)); rightDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_MIDLE_IMAGE_RIGHT_CETER_TEXT, LayoutType.RIGHTNOMALBUTTON, R.drawable.index_6_kanxw_bg, new ModuleOnClick(context, 21), R.drawable.index_6_kanxw_new, "看新闻", R.color.white)); rightDataList.add(new IndexActivityViewBean(context, ViewType.LEFT_MIDLE_IMAGE_RIGHT_CETER_TEXT, LayoutType.RIGHTBOTTPMBUTTON, R.drawable.index_8_nongjbt_bg, new ModuleOnClick(context, 17), R.drawable.index_8_nongjbt_new, "补贴一览", R.color.white)); loginReceiver = new LoginSuccessReceiver(); IntentFilter loginFilter = new IntentFilter(); loginFilter.addAction("loginSuccess"); registerReceiver(loginReceiver, loginFilter); } /** * 登录成功广播接收 登录成功后更新侧滑菜单 * * @author joe * */ public class LoginSuccessReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (null == application) { application = (MyApp) getApplicationContext(); } showTitleUI(); } } private void initWidgets() { setContentView(R.layout.activity_index_main); leftbtn = (Button) findViewById(R.id.btnBack); TextView titleTv = (TextView) findViewById(R.id.Title); leftbtn.setOnClickListener(this); titleTv.setText("农机通"); rightBtn = (Button) findViewById(R.id.btnOption); rightBtn.setOnClickListener(this); // View rlTitleBar = findViewById(R.id.titleBarStub); // final String titleString = getResources().getString(R.string.appName); // titleBar = TitleBarUtil.createTitleBar(context, rlTitleBar, // titleString, "", new OnClickListener() { // // @Override // public void onClick(View arg0) { // SlidingMenu menu = application.getsMenu(); // menu.toggle(); // } // }, "", new OnClickListener() { // // @Override // public void onClick(View arg0) { // RegisterUtils.startOtherApp(context, "管理版"); // } // }); showTitleUI(); setMsgNotice(); nullActivity_middleLinearLayout = (LinearLayout) findViewById(R.id.nullActivity_middleLinearLayout); nullActivity_leftLinearLayout = (LinearLayout) findViewById(R.id.nullActivity_leftLinearLayout); nullActivity_rightLinearLayout = (LinearLayout) findViewById(R.id.nullActivity_rightLinearLayout); rlUpdateNotice = (RelativeLayout) findViewById(R.id.rlUpdateNotice); tvUpdateTitle = (TextView) findViewById(R.id.tvUpdateTitle); ivClose = (ImageView) findViewById(R.id.ivClose); // 获取更新状态,如果有更新则显示rlUpdateNotice,点击文字后,执行更新任务 tvUpdateTitle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { doNewVersionUpdate(0); } }); ivClose.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { rlUpdateNotice.setVisibility(View.GONE); // 其他操作 } }); } private void setMsgNotice(){ if(dbhelper==null){ dbhelper = new WorkTaskNewsBeanDBHelper(IndexMainActivity.this); } List<WorkTaskBeanLocal> list = dbhelper.queryAll(); if(list!=null && list.size()>0 ){ leftbtn.setBackgroundResource(R.drawable.more_left_press_notice); }else{ leftbtn.setBackgroundResource(R.drawable.more_left_press); } } private void showTitleUI() { // Button titleRightButton = titleBar.getBtnRight(); // Button titleLeftButton = titleBar.getBtnLeft(); // titleLeftButton.setBackgroundResource(R.drawable.index_main_title_bg_left); if (application.getIsLogin()) { int width = com.jiuqi.njt.util.UIUtil .getScreenWidth(IndexMainActivity.this); String rolesString = sharePre.getPres("rolesString"); /** * 如果用户角色是管理员 并且已经登录,就显示切换到管理版按钮 */ rolesString.contains(RoleCode.Operator.getWeiyima()); String coRoleStrings = sharePre.getPres("coRoleStrings"); /** * 如果是操作员并且是管理员创建的操作员,也显示切换到管理版按钮 */ boolean isCoRole = false; if (null != coRoleStrings && (coRoleStrings.contains(RoleCode.AdminManager .getWeiyima()) || coRoleStrings.contains(RoleCode.ProvinceManager .getWeiyima()) || coRoleStrings.contains(RoleCode.CityManager .getWeiyima()) || coRoleStrings .contains(RoleCode.CountryManager.getWeiyima()))) { isCoRole = true; } if (null != rolesString && (rolesString .contains(RoleCode.AdminManager.getWeiyima()) || rolesString.contains(RoleCode.ProvinceManager .getWeiyima()) || rolesString.contains(RoleCode.CityManager .getWeiyima()) || rolesString .contains(RoleCode.CountryManager.getWeiyima())) || rolesString.contains(RoleCode.Operator.getWeiyima()) && isCoRole) { RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams( width / 7, width / 7); // para.setMargins(0, 0,(int) (width * 0.015), 0); para.addRule(RelativeLayout.CENTER_VERTICAL); para.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); rightBtn.setLayoutParams(para); rightBtn .setBackgroundResource(R.drawable.index_main_title_bg_right); if(View.VISIBLE!=rightBtn.getVisibility()){ UIUtil.showView(rightBtn); } } else { UIUtil.hideView(rightBtn); } } else { UIUtil.hideView(rightBtn); } } private void initListeners() { // 创建ViewPager adViewPager = new ViewPager(this); // // 获取屏幕像素相关信息 // DisplayMetrics dm = new DisplayMetrics(); // getWindowManager().getDefaultDisplay().getMetrics(dm); // // // 根据屏幕信息设置ViewPager广告容器的宽高 // adViewPager.setLayoutParams(new // LinearLayout.LayoutParams(dm.widthPixels,LinearLayout.LayoutParams.WRAP_CONTENT)); DisplayMetrics dm = new DisplayMetrics(); // 获取屏幕信息 getWindowManager().getDefaultDisplay().getMetrics(dm); int screenHeigh = dm.heightPixels; android.widget.RelativeLayout.LayoutParams layoutPram = new RelativeLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, (int) (screenHeigh * 0.18)); nullActivity_middleLinearLayout.setLayoutParams(layoutPram); // 将ViewPager容器设置到布局文件父容器中 nullActivity_middleLinearLayout.addView(adViewPager); initPageAdapter(); initCirclePoint(); adViewPager.setAdapter(adapter); adViewPager.setOnPageChangeListener(new AdPageChangeListener()); /** * 启动线程定时更新页面的滚动图片 */ new Thread(new Runnable() { @Override public void run() { while (true) { if (isContinue) { handler.sendEmptyMessage(atomicInteger.get()); atomicOption(); } } } }).start(); } private void initUI() { for (IndexActivityViewBean item : leftDataList) { nullActivity_leftLinearLayout.addView(item.getCreateLayout()); } for (IndexActivityViewBean item : rightDataList) { nullActivity_rightLinearLayout.addView(item.getCreateLayout()); } } /** * 检测更新 */ private void checkUpdate() { long time = Long.parseLong(sharePre.getPres("checkTime")); new CheckVersionTask(context).execute(); } public class CheckVersionTask extends AsyncTask<Void, Integer, Boolean> { private int newVerCode = 0; private String newVerName = ""; private Context currentContext; private int curVerCode; private MyApp application; public CheckVersionTask(Context context) { this.currentContext = context; this.curVerCode = Config.getVerCode(context); } private boolean getServerVerCode() { application = (MyApp) currentContext.getApplicationContext(); try { String verjson = NetworkTool.getContent(Config.UPDATE_SERVER + Config.UPDATE_VERJSON); JSONArray array = new JSONArray(verjson); if (array.length() > 0) { JSONObject obj = array.getJSONObject(0); try { newVerCode = com.jiuqi.njt.util.UIUtil.tryToInteger(obj .getString("verCode")); newVerName = obj.getString("verName"); type = UIUtil.tryToInteger(obj.getString("type")); application.setNewVername(newVerName); } catch (Exception e) { newVerCode = -1; newVerName = ""; return false; } } } catch (Exception e) { if (e != null) { } return false; } return true; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Boolean doInBackground(Void... params) { if (getServerVerCode()) { if (newVerCode > curVerCode) { return true; } } return false; } @Override protected void onPostExecute(Boolean result) { OptsharepreInterface shareInterface = new OptsharepreInterface( currentContext); shareInterface.putPres("checkTime", String.valueOf(System.currentTimeMillis())); if (result) { application.setUpdate(true); } initUI();// 重新设置ui } private void initUI() { // 有版本更新时,显示条 if (application.getIsUpdate()) { UIUtil.showView(rlUpdateNotice); if(1==type){ doNewVersionUpdate(1); } } } } private void doNewVersionUpdate(final int type) { String negativeStr = Constants.CANCEL_TEXT; if(1==type){ negativeStr = "退出"; } String verName = Config.getVerName(context); StringBuffer sb = new StringBuffer(); sb.append("当前版本:"); sb.append(verName); sb.append(",发现新版本"); sb.append(application.getNewVername()); sb.append("请下载更新"); AlertDialog d = new AlertDialog.Builder(context) .setTitle("软件更新") .setMessage(sb.toString()) .setCancelable(false) .setPositiveButton("下载", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 启动下载安装任务 // new DownLoadApkTask(context) // .execute(com.jiuqi.njt.util.UIUtil // .getNjtVo(context)); boolean isupLocStart = CheckStateInterface .isServiceRunning(context, RegisterUtils.UpdateServiceName); if (isupLocStart) { UIUtil.showMsg(context, "正在更新客户端"); } else { startService(new Intent(IndexMainActivity.this, UpdateService.class)); } // new AppFileDownUtils(context, new Handler(), // com.jiuqi.njt.update.Config.UPDATE_SERVER // + com.jiuqi.njt.update.Config.UPDATE_APKNAME, // RepairUtils.createFile(new // File("")).getName()).run(); //当点击下载的时候,把更新页面关闭 UIUtil.hideView(rlUpdateNotice); } }) .setNegativeButton(negativeStr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); if(1==type){ android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } } }).create(); d.show(); } /** * 每间隔3秒钟滚动视图显示先一个图片 如果到达页面的最后一个就重新开始计数 */ private void atomicOption() { atomicInteger.incrementAndGet(); if (atomicInteger.get() > imageViews.length - 1) { atomicInteger.getAndAdd(-(imageViews.length)); } try { Thread.sleep(3000); } catch (InterruptedException e) { } } /** * 初始化滚动页面的数据 */ private void initPageAdapter() { pageViews = new ArrayList<View>(); ImageView img1 = new ImageView(this); img1.setBackgroundResource(R.drawable.indexmain_i1); pageViews.add(img1); ImageView img2 = new ImageView(this); img2.setBackgroundResource(R.drawable.indexmain_i2); pageViews.add(img2); ImageView img3 = new ImageView(this); img3.setBackgroundResource(R.drawable.indexmain_i3); pageViews.add(img3); ImageView img4 = new ImageView(this); img4.setBackgroundResource(R.drawable.indexmain_i4); pageViews.add(img4); ImageView img5 = new ImageView(this); img5.setBackgroundResource(R.drawable.indexmain_i5); pageViews.add(img5); adapter = new AdPageAdapter(pageViews); } /** * 初始化首页滚动的园点数据 */ private void initCirclePoint() { ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup); imageViews = new ImageView[pageViews.size()]; // 广告栏的小圆点图标 for (int i = 0; i < pageViews.size(); i++) { // 创建一个ImageView, 并设置宽高. 将该对象放入到数组中 imageView = new ImageView(this); imageView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); imageViews[i] = imageView; // 初始值, 默认第0个选中 if (i == 0) { imageViews[i].setBackgroundResource(R.drawable.index_main_1); } else { imageViews[i].setBackgroundResource(R.drawable.index_main_2); } // 将小圆点放入到布局中 group.addView(imageViews[i]); } } /** * ViewPager 页面改变监听器 */ private final class AdPageChangeListener implements OnPageChangeListener { /** * 页面滚动状态发生改变的时候触发 */ @Override public void onPageScrollStateChanged(int arg0) { } /** * 页面滚动的时候触发 */ @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } /** * 页面选中的时候触发 */ @Override public void onPageSelected(int arg0) { // 获取当前显示的页面是哪个页面 atomicInteger.getAndSet(arg0); // 重新设置原点布局集合 for (int i = 0; i < imageViews.length; i++) { imageViews[arg0].setBackgroundResource(R.drawable.index_main_1); if (arg0 != i) { imageViews[i] .setBackgroundResource(R.drawable.index_main_2); } } } } @Override protected void onDestroy() { super.onDestroy(); } /* * 每隔固定时间切换广告栏图片 */ private final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { adViewPager.setCurrentItem(msg.what); super.handleMessage(msg); } }; private NjtTitleBar titleBar; private final class AdPageAdapter extends PagerAdapter { private List<View> views = null; /** * 初始化数据源, 即View数组 */ public AdPageAdapter(List<View> views) { this.views = views; } /** * 从ViewPager中删除集合中对应索引的View对象 */ @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(views.get(position)); } /** * 获取ViewPager的个数 */ @Override public int getCount() { return views.size(); } /** * 从View集合中获取对应索引的元素, 并添加到ViewPager中 */ @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(views.get(position), 0); return views.get(position); } /** * 是否将显示的ViewPager页面与instantiateItem返回的对象进行关联 这个方法是必须实现的 */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } @Override public void onClick(View v) { super.onClick(v); switch (v.getId()) { case R.id.btnBack: if(application == null){ application = (MyApp) getApplicationContext(); } SlidingMenu menu = application.getsMenu(); menu.toggle(); break; case R.id.btnOption: RegisterUtils.startOtherApp(context, "管理版"); break; default: break; } } }