package com.ure.h22h.views;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.integer;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.flurry.android.FlurryAgent;
import com.google.android.gcm.GCMRegistrar;
import com.navdrawer.SimpleSideDrawer;
import com.ure.h22h.R;
import com.ure.h22h.utils.Consts;
import com.ure.h22h.utils.MainListAdapter;
import com.ure.library.MyGenericListener;
import com.ure.library.model.AzureHelper;
import com.ure.library.model.Folder;
import com.ure.library.model.GcmHelper;
import com.ure.library.model.ViewHelper;
import eu.inmite.android.lib.dialogs.ISimpleDialogListener;
/**
- @author ted
- 主頁Activity頁面
*/
public class Main extends FragmentActivity implements ISimpleDialogListener,OnClickListener
{
private boolean _fansPage=false;
private List<Map<String, Object>> _items;
private SimpleSideDrawer _navigation;
private MainListAdapter MainListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initNavigation();
initMenu();
//顯示首頁Fragment
contrlFragment(0);
countNewList();
}
//設定導覽
private void initNavigation() {
Log.d("ted", "initNavigation");
_navigation = new SimpleSideDrawer(this);
_navigation.setLeftBehindContentView(R.layout.navigation_content);
findViewById(R.id.menu).setOnClickListener(this);
}
//預設選單設定
private void initMenu() {
Log.d("ted", "initNavigation");
_items = new ArrayList<Map<String,Object>>();
String[] menuName = getResources().getStringArray(R.array.menu_name);
int[] image = {
R.drawable.icon_star, R.drawable.icon_story, R.drawable.icon_shop,
R.drawable.icon_activity, R.drawable.icon_food, R.drawable.icon_goods,
R.drawable.icon_trip, R.drawable.icon_view, R.drawable.icon_bookmark
, R.drawable.icon_facebook
};
//Menu圖片與文字
for (int position = 0; position < menuName.length; position++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("Text", menuName[position]);
item.put("Image", image[position]);
item.put("New", "0");
_items.add(item);
}
ListView _listView = (ListView) _navigation.findViewById(R.id.menu_list);
MainListAdapter = new MainListAdapter(this,_items,image);
_listView.setAdapter(MainListAdapter);
_listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
contrlFragment(position);
_navigation.closeLeftSide();
}
});
}
//檢查是否有新東西並且顯示
private void countNewList() {
new ViewHelper(this).getFoldersAsync(new MyGenericListener<ArrayList<Folder>>() {
@Override
public void onComplete(ArrayList<Folder> eventData) {
try {
final int[] folderID = getResources().getIntArray(R.array.lib_setting_folderList);
for (int pos = 0; pos < eventData.size(); pos++){
for (int i = 0; i < folderID.length; i++) {
if(eventData.get(pos).getFolderID()==folderID[i]){
Map<String, Object> item = new HashMap<String, Object>();
item.put("Text", _items.get(i).get("Text"));
item.put("Image", _items.get(i).get("Image"));
item.put("New", eventData.get(pos).getContentCount_new().toString());
_items.set(i, item);
}
}
}
MainListAdapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onError(ArrayList<Folder> eventData) {
// TODO Auto-generated method stub
}
});
}
//控制各分類fragment
private void contrlFragment(int position) {
Log.d("ted", String.valueOf(position));
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Bundle data = new Bundle();
switch (position) {
case 0://首頁
fragmentTransaction.replace(R.id.main_pager, new FHome()).commitAllowingStateLoss();
_fansPage = false;
break;
case 9://粉絲團
data.putString("Link", "https://m.facebook.com/tainan1616");
data.putString("Title", "加入粉絲");
FWebView FWebView = new FWebView();
FWebView.setArguments(data);
fragmentTransaction.replace(R.id.main_pager,FWebView).commitAllowingStateLoss();
_fansPage = true;
break;
default://預設
Fragment CurrentFragment =null;
CurrentFragment = new FMain();
data.putInt("CurrentPage", position);
CurrentFragment.setArguments(data);
fragmentTransaction.replace(R.id.main_pager, CurrentFragment).commitAllowingStateLoss();
_fansPage = false;
break;
}
}
// 註冊 Android 設備,以便日後可以收到 PUSH 訊息
private void registerDevice(boolean allowPush) {
// 先儲存是否願意收到PUSH狀態,之後才能傳到 Server
new ViewHelper(this).setPushState(allowPush);
// 註冊 Android 設備,以便日後可以收到 PUSH 訊息
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String registationId = GCMRegistrar.getRegistrationId(this);
if (registationId.equals(""))
GCMRegistrar.register(this, Consts.GOOGLE_API_SENDERID);
else {
AzureHelper azureHelper = new AzureHelper(this);
azureHelper.registerDevice(registationId, allowPush);
}
}
private void registerDevice2(boolean allowPush) {
// 先儲存是否願意收到PUSH狀態,之後才能傳到 Server
new ViewHelper(this).setPushState(allowPush);
// 註冊 Android 設備,以便日後可以收到 PUSH 訊息
GcmHelper helper = new GcmHelper(this.getApplicationContext());
helper.startRegister(allowPush);
}
@Override
public void onBackPressed()
{
//網頁的BACK跟程式的BACK區分開來
// super.onBackPressed();
if(_fansPage)
FWebView.onBackPressed();
else
super.onBackPressed();
}
@Override
protected void onStart()
{
super.onStart();
// FlurryAgent.setLogLevel(Log.VERBOSE);
FlurryAgent.onStartSession(this, Consts.FLURRY_KEY);
}
@Override
protected void onStop()
{
super.onStop();
FlurryAgent.onEndSession(this);
}
/*
* 接收附屬Fragment中的DialogFragment裡的requestCode
* 不可寫在附屬的Fragment中會接收不到
*/
@Override
public void onPositiveButtonClicked(int requestCode) {
switch (requestCode) {
case 10:
registerDevice2(true);
break;
case 11:
final String PackgeParam = "com.ure.h22h";
try{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+PackgeParam)));
}
catch (android.content.ActivityNotFoundException anfe){
// Open Google Play website
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+PackgeParam)));
}
break;
case 12:
//do nothing
break;
case 13:
//do nothing
break;
}
}
@Override
public void onNegativeButtonClicked(int requestCode) {
switch (requestCode) {
case 10:
registerDevice2(false);
break;
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.menu:
_navigation.toggleLeftDrawer();
break;
}
}
}
this my code
first time usually is empty