com.mappn.gfan.ui.SplashActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.mappn.gfan.ui.SplashActivity.java

Source

/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * 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.mappn.gfan.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpHost;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.mappn.gfan.Constants;
import com.mappn.gfan.R;
import com.mappn.gfan.common.ApiAsyncTask.ApiRequestListener;
import com.mappn.gfan.common.HttpClientFactory;
import com.mappn.gfan.common.MarketAPI;
import com.mappn.gfan.common.util.ImageUtils;
import com.mappn.gfan.common.util.Utils;
import com.mappn.gfan.common.widget.BaseActivity;
import com.mappn.gfan.common.widget.LoadingDrawable;

/**
 * 
 * Splash Activity For GfanMobile
 * 
 * ???
 * 
 * @author andrew.wang
 * @date 2010-11-22
 * @since Version 0.4.0
 */
public class SplashActivity extends BaseActivity implements ApiRequestListener {

    private static final int VALID = 1;
    private static final int LOAD = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash_layout);

        // ?
        initSplashBg();

        final ProgressBar mLoading = (ProgressBar) findViewById(R.id.splash_loading);
        mLoading.setIndeterminateDrawable(new LoadingDrawable(getApplicationContext(), LoadingDrawable.SIZE_SMALL,
                R.color.white, R.color.splash_notification_bg, 200));

        mHandler.sendEmptyMessage(LOAD);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
     * ?
     */
    private void preload() {

        // ??
        mSession.setScreenSize(this);

        // ?
        mSession.getInstalledApps();

        if (!Utils.isNetworkAvailable(getApplicationContext())) {
            mPreloadResult = 2;
            mHandler.sendEmptyMessageDelayed(VALID, 800);
            return;
        }
        // ?CMWAP
        HttpHost proxy = Utils.detectProxy(getApplicationContext());
        if (proxy != null) {
            HttpClientFactory.get().getHttpClient().useProxyConnection(proxy);
        }

        // 
        MarketAPI.getTopRecommend(getApplicationContext(), this);
        MarketAPI.getHomeRecommend(getApplicationContext(), this, 0, 50);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case VALID:

                if (isFinishing() || mPreloadResult != 2) {
                    // wait for the preload result
                    return;
                }
                Intent i = new Intent(getApplicationContext(), HomeTabActivity.class);
                if (mContent != null) {
                    i.putExtra(Constants.EXTRA_HOME_DATA, mContent);
                }
                startActivity(i);
                finish();
                break;

            case LOAD:

                if (isFinishing()) {
                    return;
                }
                preload();
                break;

            default:
                break;
            }
        }
    };

    /*
     * ?Splash
     */
    private void initSplashBg() {

        File splashFile = new File(getApplicationContext().getCacheDir(), "splash.png");

        if (splashFile.exists()) {
            Bitmap bmp = BitmapFactory.decodeFile(splashFile.getAbsolutePath());
            if (bmp != null) {
                setSplashBitmap(bmp);
                return;
            }
        }
        // Splash
        setSplashBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.splash));
        mSession.setSplashTime(0);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onSuccess(int method, Object obj) {

        mPreloadResult++;

        if (method == MarketAPI.ACTION_GET_TOP_RECOMMEND) {

            handleTopRecommend(obj);

        } else if (method == MarketAPI.ACTION_GET_HOME_RECOMMEND) {

            handleHomeRecommend((HashMap<String, Object>) obj);

        }

        mHandler.sendEmptyMessage(VALID);
    }

    @Override
    public void onError(int method, int statusCode) {
        mPreloadResult++;
        mHandler.sendEmptyMessage(VALID);
    }

    /*
     * Splash
     */
    private void setSplashBitmap(Bitmap bmp) {

        // ????Splash?
        Bitmap scaledBitmap = ImageUtils.sacleBitmap(getApplicationContext(), bmp);
        ImageView v = ((ImageView) findViewById(R.id.iv_splashBg));
        if (scaledBitmap == null) {
            v.setImageBitmap(bmp);
        } else {
            v.setImageBitmap(scaledBitmap);
        }
    }

    /*
     * ?
     */
    private int mPreloadResult;
    /*
     * ?
     */
    private HashMap<String, Object> mContent;

    /*
     * ???
     */
    @SuppressWarnings("unchecked")
    private void handleTopRecommend(Object obj) {
        ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) obj;

        if (result == null || result.size() <= 0) {
            return;
        }
        if (mContent == null) {
            mContent = new HashMap<String, Object>();
        }
        mContent.put(Constants.EXTRA_HOME_DATA_TOP, result);
    }

    /*
     * ???
     */
    @SuppressWarnings("unchecked")
    private void handleHomeRecommend(HashMap<String, Object> obj) {

        ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) obj
                .get(Constants.KEY_PRODUCT_LIST);

        if (result == null || result.size() <= 0) {
            if (mContent != null) {
                mContent = null;
            }
            return;
        }
        if (mContent == null) {
            mContent = new HashMap<String, Object>();
        }
        mContent.put(Constants.EXTRA_HOME_DATA_BOTTOM, result);
    }

}