Skip to content

Commit f31a207

Browse files
committed
add brokenline view
1 parent 177e7ba commit f31a207

File tree

7 files changed

+103
-16
lines changed

7 files changed

+103
-16
lines changed

bin/TurtleTrade.apk

749 Bytes
Binary file not shown.

project.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
1212

1313
# Project target.
14-
target=android-17
14+
target=android-20

res/layout/stock_activity.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical" >
6+
7+
<com.stock.drawing.BrokenLineView
8+
android:id="@+id/brokenLineView1"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content" />
11+
12+
</LinearLayout>

res/values/brokenline_attr.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<declare-styleable name="BrokenLineView">
5+
<attr name="sh_stocks" format="string">
6+
600030,600036
7+
</attr>
8+
<attr name="sz_stocks" format="string">
9+
000002
10+
</attr>
11+
</declare-styleable>
12+
13+
</resources>

src/com/stock/data/StockData.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public int size() {
3838

3939
public void append(Calendar _end, YahooStock yahoo)
4040
throws IOException {
41-
String cachefile = yahoo.getCacheFile();
42-
String csvfile = yahoo.getStockFile();
43-
44-
List<List<String>> data = yahoo.get(_end, cachefile);
41+
List<List<String>> data = yahoo.get(_end, yahoo.getCacheFile());
4542
if( data == null || data.size() < 2 )
4643
return;
4744

@@ -51,17 +48,17 @@ public void append(Calendar _end, YahooStock yahoo)
5148
return;
5249

5350
Calendar _nstart = bars.get(bars.size()-1).start;
54-
Calendar _oend_ = bar_list.get(0).start;
51+
Calendar _oend = bar_list.get(0).start;
52+
if( !_oend.before(_nstart) )
53+
return;
54+
55+
Iterator<PriceBar> iter = bar_list.iterator();
56+
while( iter.hasNext() )
57+
bars.add(iter.next());
58+
bar_list = bars;
5559

56-
if( _oend_.before(_nstart) ) {
57-
Iterator<PriceBar> iter = bar_list.iterator();
58-
while( iter.hasNext() )
59-
bars.add(iter.next());
60-
bar_list = bars;
61-
62-
data = Format(bar_list);
63-
yahoo.SaveCSVFile(data, csvfile);
64-
}
60+
data = Format(bar_list);
61+
yahoo.SaveCSVFile(data, yahoo.getStockFile());
6562
}
6663

6764
public void load() {
@@ -91,6 +88,7 @@ public void load() {
9188
// download from yahoo api
9289
// if there is't local data or start date after a year age
9390
try {
91+
_stt.add(Calendar.DATE, -14);
9492
if( data == null || _stt.after(yearago) ) {
9593
data = yahoo.get(yearago, csvfile);
9694
if( data != null ) {
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.stock.drawing;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import com.stock.data.StockData;
9+
import com.stock.source.DataSource;
10+
import com.stock.turtle.R;
11+
12+
import android.content.Context;
13+
import android.content.res.TypedArray;
14+
import android.graphics.Canvas;
15+
import android.util.AttributeSet;
16+
import android.view.View;
17+
18+
public class BrokenLineView extends View {
19+
20+
List<StockData> stocks = new ArrayList<StockData>();
21+
22+
public BrokenLineView(Context context) {
23+
super(context);
24+
stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI));
25+
}
26+
27+
public BrokenLineView(Context context, AttributeSet attrs){
28+
super(context, attrs);
29+
30+
//TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
31+
//在使用完成后,一定要调用recycle方法
32+
//属性的名称是styleable中的名称+“_”+属性名称
33+
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BrokenLineView);
34+
String shstocks = array.getString(R.styleable.BrokenLineView_sh_stocks);
35+
String szstocks = array.getString(R.styleable.BrokenLineView_sz_stocks);
36+
array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
37+
38+
Pattern pattern = Pattern.compile("([\\d]{6})");
39+
if( shstocks != null && shstocks.length() > 0 ) {
40+
Matcher mCells = pattern.matcher(shstocks);
41+
while( mCells.find() ) {
42+
StockData stock = new StockData(mCells.group(), DataSource.MARKET_SHANGHAI);
43+
stocks.add(stock);
44+
}
45+
}
46+
47+
if( szstocks != null && szstocks.length() > 0 ) {
48+
Matcher mCells = pattern.matcher(szstocks);
49+
while( mCells.find() ) {
50+
StockData stock = new StockData(mCells.group(), DataSource.MARKET_SHENZHEN);
51+
stocks.add(stock);
52+
}
53+
}
54+
55+
if( stocks.size() == 0 )
56+
stocks.add(new StockData("600036", DataSource.MARKET_SHANGHAI));
57+
}
58+
59+
@Override
60+
protected void onDraw(Canvas canvas) {
61+
super.onDraw(canvas);
62+
}
63+
}

src/com/stock/source/DataSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public void download(String remoteFilePath, String localFilePath) throws IOExcep
131131
bis = new BufferedInputStream(httpUrl.getInputStream());
132132

133133
File file = new File(localFilePath);
134-
bos = new BufferedOutputStream(new FileOutputStream(file));
134+
FileOutputStream fos = new FileOutputStream(file, false);
135+
bos = new BufferedOutputStream(fos);
135136

136137
int len = 2048;
137138
byte[] b = new byte[len];

0 commit comments

Comments
 (0)