1 package org.hedgewars.hedgeroid.Downloader; |
1 package org.hedgewars.hedgeroid.Downloader; |
2 |
2 |
3 import android.app.ListActivity; |
3 import org.hedgewars.hedgeroid.R; |
|
4 |
4 import android.os.Bundle; |
5 import android.os.Bundle; |
|
6 import android.support.v4.app.FragmentActivity; |
|
7 import android.support.v4.app.FragmentManager; |
|
8 import android.support.v4.app.FragmentTransaction; |
|
9 import android.view.Gravity; |
5 import android.view.View; |
10 import android.view.View; |
6 import android.widget.AdapterView; |
11 import android.view.View.OnClickListener; |
7 import android.widget.AdapterView.OnItemClickListener; |
12 import android.widget.Button; |
8 import android.widget.ArrayAdapter; |
13 import android.widget.FrameLayout; |
|
14 import android.widget.ImageView; |
|
15 import android.widget.LinearLayout; |
9 |
16 |
10 public class DownloadListActivity extends ListActivity implements OnItemClickListener{ |
17 public class DownloadListActivity extends FragmentActivity implements OnItemMovementListener{ |
11 |
18 |
12 |
19 private FrameLayout layout = null; |
|
20 private LinearLayout downloadQueueContainer = null; |
|
21 private View infoView = null; |
|
22 private ImageView arrow = null; |
|
23 private int halfSize = 0; |
|
24 private DownloadPackage task = null; |
|
25 |
13 public void onCreate(Bundle savedInstanceState){ |
26 public void onCreate(Bundle savedInstanceState){ |
14 super.onCreate(savedInstanceState); |
27 super.onCreate(savedInstanceState); |
15 |
28 |
16 DownloadTask[] tasks = new DownloadTask[3]; |
29 setContentView(R.layout.download_listactivity); |
17 tasks[0] = new DownloadTask("url1", "/home/path/1", 1, "entry 1"); |
30 //FragmentManager fm = this.getSupportFragmentManager(); |
18 tasks[1] = new DownloadTask("url2", "/home/path/2", 1, "entry 2"); |
31 //if(fm.findFragmentById(android.R.id.content) == null){ |
19 tasks[2] = new DownloadTask("url3", "/home/path/3", 1, "entry 3"); |
32 // DownloadListFragment listfrag = new DownloadListFragment(); |
20 |
33 // fm.beginTransaction().add(android.R.id.content, listfrag).commit(); |
21 ArrayAdapter<DownloadTask> adapter = new ArrayAdapter<DownloadTask>(this, android.R.layout.simple_list_item_1, tasks); |
34 //} |
22 this.setListAdapter(adapter); |
35 |
23 this.getListView().setOnItemClickListener(this); |
36 layout = (FrameLayout)findViewById(R.id.downloadFrameLayout); |
24 |
37 downloadQueueContainer = (LinearLayout) findViewById(R.id.downloadQueueContainer); |
|
38 } |
|
39 |
|
40 public void onNewItemSelected(DownloadPackage _task, int x, int minX, int maxX, int size) { |
|
41 if(layout != null){ |
|
42 if(!_task.equals(task)){//if it's a new task refresh the whole thing |
|
43 task = _task; |
|
44 layout.removeView(infoView); |
|
45 infoView = null; |
|
46 } |
|
47 |
|
48 if(infoView == null){//build iv if it hasn't been created yet |
|
49 infoView = this.getLayoutInflater().inflate(R.layout.download_info, layout, false); |
|
50 FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) infoView.getLayoutParams(); |
|
51 params.gravity = Gravity.NO_GRAVITY; |
|
52 params.height = size; |
|
53 arrow = (ImageView)infoView.findViewById(R.id.arrow); |
|
54 arrow.setVisibility(View.INVISIBLE); |
|
55 halfSize = size/2; |
|
56 |
|
57 Button yes = (Button)infoView.findViewById(R.id.download); |
|
58 Button no = (Button)infoView.findViewById(R.id.cancel); |
|
59 yes.setOnClickListener(yesClicker); |
|
60 no.setOnClickListener(noClicker); |
|
61 |
|
62 layout.addView(infoView, params); |
|
63 } |
|
64 } |
|
65 } |
|
66 |
|
67 public void onViewMoved(int x, int minX, int maxX) { |
|
68 if(halfSize == -1){ |
|
69 if(infoView.getHeight() != 0){ |
|
70 halfSize = infoView.getHeight()/2; |
|
71 } |
|
72 } |
|
73 if(layout != null && infoView != null){ |
|
74 FrameLayout.LayoutParams params = ((FrameLayout.LayoutParams)infoView.getLayoutParams()); |
|
75 if(x - halfSize < minX){ |
|
76 params.topMargin = 0; |
|
77 arrow.setVisibility(View.INVISIBLE); |
|
78 params.gravity = Gravity.TOP; |
|
79 }else if (x + halfSize >= maxX){ |
|
80 params.topMargin = 0; |
|
81 arrow.setVisibility(View.INVISIBLE); |
|
82 params.gravity = Gravity.BOTTOM; |
|
83 }else{ |
|
84 params.topMargin = x - halfSize; |
|
85 params.gravity = Gravity.NO_GRAVITY; |
|
86 arrow.setVisibility(View.VISIBLE); |
|
87 } |
|
88 |
|
89 infoView.requestLayout(); |
|
90 |
|
91 } |
25 } |
92 } |
26 |
93 |
27 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { |
94 private OnClickListener yesClicker = new OnClickListener(){ |
28 DownloadTask task = (DownloadTask)arg0.getAdapter().getItem(position); |
95 public void onClick(View v){ |
29 } |
96 FragmentManager fm = getSupportFragmentManager(); |
|
97 FragmentTransaction ft= fm.beginTransaction(); |
|
98 DownloadFragment df = DownloadFragment.getInstance(task); |
|
99 |
|
100 ft.add(R.id.downloadQueueContainer, df).commit(); |
|
101 //ft.show(new DownloadFragment()); |
|
102 |
|
103 task = null; |
|
104 layout.removeView(infoView); |
|
105 } |
|
106 }; |
30 |
107 |
31 // public static class Dialog extends DialogFragment{ |
108 private OnClickListener noClicker = new OnClickListener(){ |
32 |
109 public void onClick(View v){ |
33 // } |
110 task = null; |
34 |
111 layout.removeView(infoView); |
|
112 } |
|
113 }; |
35 } |
114 } |
36 |
|