首页>代码>Android商业项目多功能媒体完整版APP源代码>/telecapoland-jamendo-android-6cd07fb/src/com/teleca/jamendo/activity/DownloadActivity.java
/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.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.teleca.jamendo.activity;

import java.util.ArrayList;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.adapter.DownloadJobAdapter;
import com.teleca.jamendo.util.download.DownloadInterface;
import com.teleca.jamendo.util.download.DownloadJob;
import com.teleca.jamendo.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author Lukasz Wisniewski
 */
public class DownloadActivity extends Activity {
	
	/**
     * Runnable periodically querying DownloadService about
     * downloads
     */
    private Runnable mUpdateTimeTask = new Runnable() {
            public void run() {
            	updateListView(mDownloadSpinner.getSelectedItemPosition());
            	mHandler.postDelayed(this, 1000);
            }
    };
    
    private Handler mHandler;
	
	private Spinner mDownloadSpinner;
	private TextView mItemCountTextView;
	private ListView mListView;
	private ViewFlipper mViewFlipper;
	
	private DownloadInterface mDownloadInterface;
	
	/**
	 * Launch this Activity from the outside
	 *
	 * @param c context from which Activity should be started
	 */
	public static void launch(Context c){
		Intent intent = new Intent(c, DownloadActivity.class);
		c.startActivity(intent);
	}
	
	/** Called when the activity is first created. */
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.download);
		
		mDownloadInterface = JamendoApplication.getInstance().getDownloadInterface();
		
		mItemCountTextView = (TextView)findViewById(R.id.ItemsCountTextView);
		
		mDownloadSpinner = (Spinner)findViewById(R.id.DownloadSpinner);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(
				this, R.array.download_modes, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mDownloadSpinner.setAdapter(adapter);
		mDownloadSpinner.setOnItemSelectedListener(mOnItemSelectedListener);
		
		mListView = (ListView) findViewById(R.id.DownloadListView);
		mViewFlipper = (ViewFlipper) findViewById(R.id.DownloadViewFlipper);
		
		mHandler = new Handler();
	}
	
	@Override
	protected void onPause() {
		mHandler.removeCallbacks(mUpdateTimeTask);
		super.onPause();
	}

	@Override
	protected void onResume() {
		mHandler.postDelayed(mUpdateTimeTask, 1000);
		super.onResume();
	}

	/**
	 * Spinner select action, display all, complete or queue downloads
	 */
	private OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener(){

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			updateListView(position);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
		
	};
	
	private int lastSpinnerPosition = -1;
	
	private void updateListView(int position){
		if(position == lastSpinnerPosition){
			DownloadJobAdapter adapter = (DownloadJobAdapter)mListView.getAdapter();
			adapter.notifyDataSetChanged();
			return;
		}
		
		ArrayList<DownloadJob> jobs = null;
		switch (position) {
		case 0:
			// Display ALL
			jobs = mDownloadInterface.getAllDownloads();
			break;
			
		case 1:
			// Display Completed
			jobs = mDownloadInterface.getCompletedDownloads();
			break;
			
		case 2:
			// Display Queued
			jobs = mDownloadInterface.getQueuedDownloads();
			break;

		default:
			break;
		}
		
		if(jobs != null){
			mItemCountTextView.setText(jobs.size()+" "+getString(R.string.items));
			
			DownloadJobAdapter adapter = new DownloadJobAdapter(DownloadActivity.this);
			adapter.setList(jobs);
			mListView.setAdapter(adapter);
		}
		
		lastSpinnerPosition = position;
		setupListView();
	}
	
	private void setupListView(){
		if(mListView.getCount() > 0){
			mViewFlipper.setDisplayedChild(0);
		} else {
			mViewFlipper.setDisplayedChild(1);
		}
	}

}
最近下载更多
haozhilang  LV8 2023年11月2日
wzm123530  LV4 2023年5月30日
moomin709  LV24 2022年2月28日
木木不爱安妮酱  LV10 2022年1月7日
oopocia  LV4 2021年5月26日
香菜  LV1 2021年5月11日
LogCat  LV1 2021年3月26日
wcyzm999  LV4 2021年1月13日
123211234567  LV1 2021年1月8日
18781170695  LV6 2020年12月30日
最近浏览更多
暂无贡献等级
666ing  LV2 2023年12月27日
不止是六位数  LV7 2023年12月15日
haozhilang  LV8 2023年10月31日
tianshi  LV7 2023年10月19日
dzlwindy  LV8 2023年8月28日
17609102982 2023年8月22日
暂无贡献等级
Yanguu 2023年7月11日
暂无贡献等级
孙煜皓 2023年6月28日
暂无贡献等级
itlaolang  LV5 2023年6月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友