package
com.again.studyenglish1;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
android.app.Activity;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteException;
import
android.os.Bundle;
import
android.os.Handler;
import
android.view.View;
import
android.view.WindowManager;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
FlashingWords2
extends
Activity {
private
static
String DB_PATH =
"/sdcard/"
;
private
static
String DB_NAME =
"dictionary.sqlite"
;
private
int
listcount =
0
;
private
int
currentPos =
0
;
private
boolean
isPlaying =
false
;
private
String[] wordList =
null
;
private
String[] definitionList =
null
;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.flashingwords2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
loadData();
Button startButton = (Button)findViewById(R.id.startbtn2);
startButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
isPlaying =
true
;
}
});
Button stopButton = (Button)findViewById(R.id.stopbtn2);
stopButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
isPlaying =
false
;
}
});
setWord();
}
public
void
setWordData()
{
if
(isPlaying ==
true
)
{
TextView wordText = (TextView)findViewById(R.id.word2);
TextView definitionText = (TextView)findViewById(R.id.definition2);
wordText.setText(wordList[currentPos]);
definitionText.setText(definitionList[currentPos]);
currentPos++;
if
(currentPos >= listcount)
{
currentPos =
0
;
}
}
}
public
void
setWord()
{
setWordData();
Handler mHandler =
new
Handler();
mHandler.postDelayed(
new
Runnable()
{
@Override
public
void
run()
{
setWord();
}
},
1000
);
}
private
boolean
checkDatabase()
{
SQLiteDatabase checkDB =
null
;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath,
null
, SQLiteDatabase.OPEN_READONLY);
}
catch
(SQLiteException e){}
if
(checkDB !=
null
)
{
checkDB.close();
}
return
checkDB !=
null
?
true
:
false
;
}
private
void
copyDatabase()
throws
IOException
{
InputStream myInput =
this
.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput =
new
FileOutputStream(outFileName);
byte
[] buffer =
new
byte
[
1024
];
int
total_length =
0
;
int
length;
while
((length = myInput.read(buffer))>
0
);
{
myOutput.write(buffer,
0
, length);
total_length += length;
}
total_length += length;
myOutput.flush();
myOutput.close();
myInput.close();
}
public
void
createDatabase()
throws
IOException
{
boolean
dbExist = checkDatabase();
if
(dbExist){}
else
{
try
{
copyDatabase();
}
catch
(IOException e)
{
throw
new
Error(
"DB를 복사하지 못했습니다!"
);
}
}
}
public
void
loadData()
{
try
{
createDatabase();
}
catch
(IOException e)
{
Toast.makeText(
this
,
"DB 파일을 생성할 수 없습니다."
, Toast.LENGTH_LONG).show();
}
listcount =
0
;
try
{
Cursor cursor;
SQLiteDatabase db =
SQLiteDatabase.openDatabase(DB_PATH + DB_NAME,
null
,
1
);
String[] FROM = {
"*"
};
cursor = db.query(
"vocabulary2"
, FROM,
null
,
null
,
null
,
null
,
null
);
startManagingCursor(cursor);
while
(cursor.moveToNext())
{
wordList[listcount] = cursor.getString(
0
);
definitionList[listcount] = cursor.getString(
1
);
listcount++;
}
if
(db !=
null
)
db.close();
}
catch
(Exception e)
{
Toast.makeText(
this
,
"에러 : "
+ e.toString(), Toast.LENGTH_LONG).show();
}
}
}