master
commit
529337c81d
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1 +0,0 @@
|
||||
1
|
Binary file not shown.
Binary file not shown.
@ -1 +0,0 @@
|
||||
1
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 451 KiB |
@ -1 +0,0 @@
|
||||
1
|
@ -0,0 +1,15 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/caches
|
||||
/.idea/libraries
|
||||
/.idea/modules.xml
|
||||
/.idea/workspace.xml
|
||||
/.idea/navEditor.xml
|
||||
/.idea/assetWizardSettings.xml
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.externalNativeBuild
|
||||
.cxx
|
||||
local.properties
|
@ -0,0 +1 @@
|
||||
SleepHelper
|
@ -1,3 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,38 @@
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'com.example.sleephelper'
|
||||
compileSdk 33
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.sleephelper"
|
||||
minSdk 31
|
||||
targetSdk 33
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.4.1'
|
||||
implementation 'com.google.android.material:material:1.5.0'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.sleep">
|
||||
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.SENSOR_INFO" />
|
||||
<uses-permission android:name="android.permission.SENSOR_ENABLE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
|
||||
<uses-feature
|
||||
android:name="android.hardware.camera"
|
||||
android:required="false" />
|
||||
|
||||
|
||||
<application
|
||||
android:name="com.example.sleep.App"
|
||||
android:allowBackup="false"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".SplashActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".activity.LoginActivity"
|
||||
android:theme="@style/Theme.NoAction"/>
|
||||
|
||||
<activity
|
||||
android:name=".activity.RegisterActivity"
|
||||
android:theme="@style/Theme.NoAction"/>
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.MainScreen"
|
||||
|
||||
android:launchMode="standard"
|
||||
android:theme="@style/Theme.NoAction" />
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.Record"
|
||||
android:launchMode="standard" />
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.SleepresultScreen"
|
||||
android:launchMode="standard" />
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.SleepMonitoringScreen"
|
||||
android:launchMode="standard"
|
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.AfterSleep"
|
||||
android:launchMode="singleTask" />
|
||||
|
||||
<activity
|
||||
android:name="com.example.sleep.CalendarPage"
|
||||
android:theme="@style/Theme.NoAction" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.DaemonService"
|
||||
android:enabled="true"
|
||||
android:process=":daemon_service" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.SensorService"
|
||||
android:process=":work" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.GoSleepService"
|
||||
android:enabled="true"
|
||||
android:process=":go_sleep" />
|
||||
|
||||
<receiver android:name="com.example.sleep.GoSleepBroadcastReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.PlayerMusicService"
|
||||
android:enabled="true"
|
||||
android:process=":music_service" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.MediaService"
|
||||
android:enabled="true" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.AliveJobService"
|
||||
android:permission="android.permission.BIND_JOB_SERVICE" />
|
||||
|
||||
<service
|
||||
android:name="com.example.sleep.service.GrayService"
|
||||
android:enabled="true"
|
||||
android:process=":gray_service" />
|
||||
|
||||
<meta-data
|
||||
android:name="preloaded_fonts"
|
||||
android:resource="@array/preloaded_fonts" />
|
||||
|
||||
<activity
|
||||
android:name=".EditScreen"/>
|
||||
|
||||
<receiver android:name=".AlarmReceiver" />
|
||||
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,37 @@
|
||||
package com.example;
|
||||
//导入所需的类和接口
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
public class ViewPagerAdapter extends FragmentPagerAdapter {
|
||||
private List<Fragment> fragments = new ArrayList<>();
|
||||
private List<String> fragmentTitles = new ArrayList<>();
|
||||
//两个列表将会存储即将被加入到ViewPager中的 Fragment 和 页面标题。
|
||||
public ViewPagerAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
public void addFragment(Fragment fragment, String title) {
|
||||
fragments.add(fragment);
|
||||
fragmentTitles.add(title);
|
||||
}//用于添加 Fragment 和对应的标题到 fragments 和 fragmentTitles 列表中。
|
||||
//重写 getItem() 方法是为了返回 ViewPager 中要求位置的 Fragment 实例。
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
return fragments.get(position);
|
||||
}
|
||||
//getCount() 返回在适配器中的 Fragment 数量。
|
||||
@Override
|
||||
public int getCount() {
|
||||
return fragments.size();
|
||||
}
|
||||
//getPageTitle() 方法返回在 ViewPager 中要求位置的页面标题。
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return fragmentTitles.get(position);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,116 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.drawChart.DrawPieChart;
|
||||
import com.github.mikephil.charting.charts.PieChart;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 结束记录的界面
|
||||
*/
|
||||
public class AfterSleepReport extends Activity {
|
||||
|
||||
//这些变量存储对布局文件中的UI元素的引用
|
||||
RelativeLayout background; //背景图片布局
|
||||
TextView mTime; //显示总的睡眠时间的文字视图
|
||||
TextView mStartTime; //显示开始睡眠时间的文字视图
|
||||
TextView mStopTime; //显示结束睡眠时间的文字视图
|
||||
PieChart mPieChart; //用于显示深度、浅度和REM睡眠时间的饼状图
|
||||
TextView mDeep; //显示深度睡眠时间的文字视图
|
||||
TextView mSwallow; //显示浅度睡眠时间的文字视图
|
||||
TextView mDream; //显示睡眠时间的文字视图
|
||||
DrawPieChart mDrawPieChart; //显示睡眠信息图
|
||||
|
||||
private RecordBean mRecord;
|
||||
private ImageView sleepReport;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
//调用父类的方法,设置默认的布局文件
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.aftersleep);
|
||||
|
||||
//初始化各个UI组件
|
||||
background = findViewById(R.id.aftersleep_background);
|
||||
findViewById(R.id.btn_AfterSleep).setOnClickListener(v -> {
|
||||
finish(); //当点击"完成"按钮时,结束当前活动
|
||||
});
|
||||
sleepReport = findViewById(R.id.report);
|
||||
mPieChart = findViewById(R.id.mPiechart);
|
||||
mTime = findViewById(R.id.sleepTime);
|
||||
mStartTime = findViewById(R.id.startTime);
|
||||
mStopTime = findViewById(R.id.stopTime);
|
||||
mDeep = findViewById(R.id.deep);
|
||||
mSwallow = findViewById(R.id.swallow);
|
||||
mDream = findViewById(R.id.dream);
|
||||
|
||||
//随机背景
|
||||
int[] array = {R.drawable.bg_1, R.drawable.bg_4, R.drawable.bg_5};
|
||||
Random rnd = new Random();
|
||||
int index = rnd.nextInt(3);
|
||||
Resources resources = getBaseContext().getResources();
|
||||
Drawable cur = resources.getDrawable(array[index]);
|
||||
background.setBackground(cur);
|
||||
//long recordId = this.getIntent().getLongExtra("recordId", 0);
|
||||
//获取最新的睡眠记录
|
||||
GetRecord mGetRecord = getRecord();
|
||||
mRecord = mGetRecord.getLatestRecord();
|
||||
initView();
|
||||
|
||||
}
|
||||
|
||||
//设置文本
|
||||
protected void initView() {
|
||||
if (mRecord != null) {
|
||||
mStartTime.setText(String.format(getResources().getString(R.string.sleep_time), mRecord.getStartTime()));
|
||||
mStopTime.setText(String.format(getResources().getString(R.string.get_up_time), mRecord.getEndTime()));
|
||||
mTime.setText(String.format(Locale.getDefault(), "时长 %02d:%02d",
|
||||
mRecord.getTotalTime() / 60, mRecord.getTotalTime() % 60));
|
||||
mDeep.setText(String.format(Locale.getDefault(), "深度睡眠 %02d:%02d",
|
||||
mRecord.getDeepTime() / 60, mRecord.getDeepTime() % 60));
|
||||
mSwallow.setText(String.format(Locale.getDefault(), "浅层睡眠 %02d:%02d",
|
||||
mRecord.getSwallowTime() / 60, mRecord.getSwallowTime() % 60));
|
||||
mDream.setText(String.format(Locale.getDefault(), "醒/梦 %02d:%02d",
|
||||
mRecord.getAwakeTime() / 60, mRecord.getAwakeTime() % 60));
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
} else {
|
||||
Toast.makeText(this, "数据错误", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
//返回键效果
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||
Intent i = new Intent().setClass(AfterSleepReport.this, MainScreen.class);
|
||||
AfterSleepReport.this.startActivity(i);
|
||||
AfterSleepReport.this.finish();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.Log;
|
||||
|
||||
public class AlarmReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// 获取默认的闹钟铃声的URI
|
||||
// Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
||||
// Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
||||
Log.e("闹铃:","received: " +alarmTone.toString());
|
||||
// 检查权限
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
||||
// 使用Ringtone来播放声音
|
||||
Ringtone ringtone = RingtoneManager.getRingtone(App.mContext, alarmTone);
|
||||
if (ringtone != null) {
|
||||
Log.e("闹铃:","ringtone is playing");
|
||||
ringtone.play();
|
||||
} else {
|
||||
Log.e("闹铃:","ringtone is null");
|
||||
}
|
||||
} else {
|
||||
// 如果没有权限,可以选择播放内置的声音或其他方式提醒用户
|
||||
Log.e("闹铃:","no premission");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.sleep.database.dao.DatabaseManager;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class EditScreen extends FragmentActivity {
|
||||
private ImageView showImage;
|
||||
private ImageView postImage;
|
||||
private String imageUrl;
|
||||
private EditText messageEdit;
|
||||
private Bitmap bitmap;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.event_content_activity);
|
||||
|
||||
Bundle extras = getIntent().getExtras();
|
||||
boolean isPhoto = getIntent().getBooleanExtra("isPhoto", false);
|
||||
|
||||
postImage = findViewById(R.id.event_content_post);
|
||||
messageEdit = findViewById(R.id.event_content);
|
||||
showImage = findViewById(R.id.event_image);
|
||||
|
||||
// 根据传入的数据决定是否显示图片
|
||||
if (isPhoto) {
|
||||
imageUrl = getIntent().getStringExtra("bitmapImageString");
|
||||
Bitmap bitmap = BitmapFactory.decodeFile(imageUrl);
|
||||
showImage.setImageBitmap(bitmap);
|
||||
} else {
|
||||
if (extras != null) {
|
||||
bitmap = extras.getParcelable("bitmapImage");
|
||||
if (bitmap != null) {
|
||||
showImage.setImageBitmap(bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
postImage.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String message = messageEdit.getText().toString();
|
||||
if (TextUtils.isEmpty(message)) {
|
||||
Toast.makeText(EditScreen.this, "请添加消息", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// 从SharedPreferences获取用户名
|
||||
SharedPreferences sharedPreferences = EditScreen.this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
|
||||
String retrievedUserName = sharedPreferences.getString("UserNameKey", "DefaultUserName");
|
||||
|
||||
// 打开数据库管理器
|
||||
DatabaseManager manager = new DatabaseManager(EditScreen.this);
|
||||
manager.open();
|
||||
|
||||
// 根据是否是照片贴文,将消息和图片信息保存到数据库
|
||||
if (isPhoto) {
|
||||
manager.postMood(retrievedUserName, message, imageUrl);
|
||||
} else {
|
||||
String bitmapUrl = bitmapToBase64(bitmap);
|
||||
manager.postMood(retrievedUserName, message, bitmapUrl);
|
||||
}
|
||||
|
||||
// 关闭数据库管理器
|
||||
manager.close();
|
||||
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 将Bitmap转换为Base64编码的字符串
|
||||
public String bitmapToBase64(Bitmap bitmap) {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
||||
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
||||
return Base64.encodeToString(byteArray, Base64.DEFAULT);
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
// 事件类
|
||||
public class Event implements Serializable {
|
||||
public String id; // 事件的唯一标识符
|
||||
public String title; // 事件的标题
|
||||
public String des; // 事件的描述
|
||||
public String imageUrl; // 事件的图片URL
|
||||
public List<EventContent> contents; // 包含事件内容的列表
|
||||
|
||||
// 事件类构造函数
|
||||
public Event(String id, String title, String des, String imageUrl, List<EventContent> contents) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.des = des;
|
||||
this.imageUrl = imageUrl;
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
// 无参构造函数
|
||||
public Event() {
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDes() {
|
||||
return des;
|
||||
}
|
||||
|
||||
public void setDes(String des) {
|
||||
this.des = des;
|
||||
}
|
||||
|
||||
public String getImageUrl() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
public void setImageUrl(String imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
public List<EventContent> getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
public void setContents(List<EventContent> contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
}
|
||||
|
||||
// 事件内容类
|
||||
class EventContent implements Serializable {
|
||||
public String contentId; // 内容的唯一标识符
|
||||
public String userId; // 用户的唯一标识符,与事件内容关联
|
||||
public String content; // 事件的具体内容
|
||||
public String userName; // 用户的名字
|
||||
|
||||
// 事件内容类构造函数
|
||||
public EventContent(String contentId, String userId, String content, String userName) {
|
||||
this.contentId = contentId;
|
||||
this.userId = userId;
|
||||
this.content = content;
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
// 无参构造函数
|
||||
public EventContent() {
|
||||
}
|
||||
|
||||
public String getContentId() {
|
||||
return contentId;
|
||||
}
|
||||
|
||||
public void setContentId(String contentId) {
|
||||
this.contentId = contentId;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.example.sleep;
|
||||
|
||||
public class ItemModel {
|
||||
private String imageUrl; // 用于存储图像URL或资源标识符
|
||||
private String text; // 用于存储文本信息
|
||||
|
||||
// 构造函数,用于创建 ItemModel 对象并初始化其属性
|
||||
public ItemModel(String imageUrl, String text) {
|
||||
this.imageUrl = imageUrl;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
// 获取图像URL或资源标识符的方法
|
||||
public String getImageResId() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
// 获取文本信息的方法
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
import com.example.ViewPagerAdapter;
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.service.GoSleepService;
|
||||
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
|
||||
/**
|
||||
* 一开始的主界面
|
||||
*/
|
||||
public class MainScreen extends FragmentActivity {
|
||||
|
||||
private static final int REQUEST_READ_EXTERNAL_STORAGE = 1;
|
||||
private long exitTime = 0;
|
||||
private ViewPager viewPager;
|
||||
private TabLayout tabLayout;
|
||||
private Button backRl;
|
||||
|
||||
|
||||
//初始化
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
readLog();
|
||||
/*initView();*/
|
||||
startGoSleepService();
|
||||
|
||||
viewPager = findViewById(R.id.viewPager);
|
||||
backRl = findViewById(R.id.btn_AfterSleep);
|
||||
backRl.setOnClickListener(v -> finish());
|
||||
tabLayout = findViewById(R.id.tabLayout);
|
||||
|
||||
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
|
||||
adapter.addFragment(new SleepFragment(), "睡眠");
|
||||
adapter.addFragment(new DreamAnalyszeScreen(), "解梦");
|
||||
adapter.addFragment(new Share(), "分享");
|
||||
adapter.addFragment(new SuggestScreen(), "报告");
|
||||
|
||||
viewPager.setAdapter(adapter);
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
|
||||
// 检查权限
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
// 请求权限
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
|
||||
if (requestCode == REQUEST_READ_EXTERNAL_STORAGE) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// 权限已获得
|
||||
} else {
|
||||
// 权限被拒绝
|
||||
Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//按钮
|
||||
public void ClickRecord(View v) {
|
||||
Intent i = new Intent();
|
||||
i.setClass(MainScreen.this, CalendarPage.class);
|
||||
MainScreen.this.startActivity(i);
|
||||
MainScreen.this.finish();
|
||||
}
|
||||
|
||||
//按下返回键的效果
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
|
||||
if (System.currentTimeMillis() - exitTime > 2000) {
|
||||
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
|
||||
exitTime = System.currentTimeMillis();
|
||||
} else {
|
||||
finish();
|
||||
System.exit(0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
//读睡眠记录,判断是否异常退出
|
||||
private void readLog() {
|
||||
RecordBean mRecord;
|
||||
GetRecord mGetRecord = getRecord();
|
||||
mRecord = mGetRecord.getLatestRecord();
|
||||
if (mRecord != null) {
|
||||
if (!mRecord.getValid()) {
|
||||
Intent i = new Intent();
|
||||
i.setClass(MainScreen.this, SleepMonitoringScreen.class);
|
||||
MainScreen.this.startActivity(i);
|
||||
MainScreen.this.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void startGoSleepService() {
|
||||
Intent ifSleepIntent = new Intent(this, GoSleepService.class);
|
||||
this.startService(ifSleepIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.recyclerView.Trace;
|
||||
import com.example.sleep.recyclerView.TraceListAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
/**
|
||||
* 睡眠记录页面
|
||||
*/
|
||||
public class Record extends Activity {
|
||||
|
||||
private RecyclerView rvTrace;
|
||||
private List<Trace> traceList = new ArrayList<>();//睡眠记录列表
|
||||
|
||||
private String date = "";//日期
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.records);
|
||||
rvTrace = findViewById(R.id.timelList);
|
||||
date = this.getIntent().getStringExtra("date");
|
||||
initData();
|
||||
}
|
||||
|
||||
//睡眠记录数据初始化
|
||||
private void initData() {
|
||||
GetRecord mGetRecord = getRecord();//获取数据库操作对象
|
||||
//List<RecordBean> records = mGetRecord.queryAllList();
|
||||
List<RecordBean> records = mGetRecord.queryByDate(date);// 根据日期查询睡眠记录
|
||||
if (records.size() == 1)
|
||||
{
|
||||
//如果只有一条记录,直接跳转到记录详情页面
|
||||
Intent i = new Intent(Record.this, SleepresultScreen.class);
|
||||
i.putExtra("date", date);
|
||||
i.putExtra("position", 0);
|
||||
Record.this.startActivity(i);
|
||||
Record.this.finish();
|
||||
} else {
|
||||
//如果有多条记录,将记录添加到列表中,并设置 RecyclerView 的适配器
|
||||
for (RecordBean e : records) {
|
||||
traceList.add(new Trace(e.getDate(), e.getStartTime() + "-" + e.getEndTime()
|
||||
+ " " + e.getTotalTime() / 60 + "时" + e.getTotalTime() % 60 + "分"));
|
||||
}
|
||||
TraceListAdapter adapter = new TraceListAdapter(this, traceList, date);
|
||||
rvTrace.setLayoutManager(new LinearLayoutManager(this));
|
||||
rvTrace.setAdapter(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
//左上的返回
|
||||
public void ClickBack(View v) {
|
||||
Record.this.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.example.sleep;
|
||||
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TimePicker;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Random;
|
||||
|
||||
public class SleepFragment extends Fragment {
|
||||
private Button startSleep;
|
||||
private FragmentActivity activity;
|
||||
private Button alarmTime;
|
||||
private Calendar calendar;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// 在这里设置 SleepFragment 的布局,例如从 XML 文件中加载
|
||||
View view = inflater.inflate(R.layout.fragment_sleep, container, false);
|
||||
initView(view);
|
||||
initListener();
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
// 在这里获取关联的Activity上下文
|
||||
activity = getActivity();
|
||||
}
|
||||
|
||||
private void initListener() {
|
||||
startSleep.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Toast.makeText(App.mContext, "开始记录!", Toast.LENGTH_SHORT).show();
|
||||
Intent i = new Intent();
|
||||
i.setClass(App.mContext, SleepMonitoringScreen.class);
|
||||
activity.startActivity(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
public void initView(View view) {
|
||||
ImageView mImageText;
|
||||
mImageText = view.findViewById(R.id.imageText);
|
||||
startSleep = view.findViewById(R.id.btn_start);
|
||||
alarmTime = view.findViewById(R.id.timeBtn);
|
||||
alarmTime.setText("我将会在" + " 00:00 " + "醒来");
|
||||
alarmTime.setOnClickListener(new Button.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View arg0) {
|
||||
calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||
new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
|
||||
@Override
|
||||
public void onTimeSet(TimePicker arg0, int h, int m) {
|
||||
MySharedPreferences.saveTime(App.mContext,h,m);
|
||||
//更新按钮上的时间
|
||||
alarmTime.setText("我将会在 " + formatTime(h, m) + " " + "醒来");
|
||||
//设置日历的时间,主要是让日历的年月日和当前同步
|
||||
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||
//设置日历的小时和分钟
|
||||
calendar.set(Calendar.HOUR_OF_DAY, h);
|
||||
calendar.set(Calendar.MINUTE, m);
|
||||
//将秒和毫秒设置为0
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
}
|
||||
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
|
||||
}
|
||||
});
|
||||
//设置随机背景
|
||||
int[] array = {R.drawable.main_bg_1, R.drawable.main_bg_2, R.drawable.main_bg_3,
|
||||
R.drawable.main_bg_4, R.drawable.main_bg_5, R.drawable.main_bg_6};
|
||||
Random rnd = new Random();
|
||||
int index = rnd.nextInt(6);
|
||||
Resources resources = getActivity().getResources();
|
||||
Drawable cur = resources.getDrawable(array[index]);
|
||||
mImageText.setBackground(cur);
|
||||
}
|
||||
|
||||
public String formatTime(int h, int m) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
if (h < 10) {
|
||||
buf.append("0" + h);
|
||||
} else {
|
||||
buf.append(h);
|
||||
}
|
||||
buf.append(" : ");
|
||||
if (m < 10) {
|
||||
buf.append("0" + m);
|
||||
} else {
|
||||
buf.append(m);
|
||||
}
|
||||
setAlarm(getActivity(),h,m);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public void setAlarm(Context context, int h, int m) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(context, AlarmReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||
calendar.set(Calendar.HOUR_OF_DAY, h);
|
||||
calendar.set(Calendar.MINUTE, m);
|
||||
|
||||
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
package com.example.sleep;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
import com.github.mikephil.charting.charts.PieChart;
|
||||
import com.example.sleep.database.GetRecord;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.drawChart.DrawLineChart;
|
||||
import com.example.sleep.drawChart.DrawPieChart;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import static com.example.sleep.database.GetRecord.getRecord;
|
||||
|
||||
/**
|
||||
* 详细的记录页面
|
||||
*/
|
||||
public class SleepresultScreen extends Activity {
|
||||
|
||||
Button btn_left;
|
||||
Button btn_right;
|
||||
TextView mDate;
|
||||
TextView mStartTime;
|
||||
TextView mStopTime;
|
||||
TextView mSleepTime;
|
||||
TextView mDeep;
|
||||
TextView mSwallow;
|
||||
TextView mDream;
|
||||
LineChart mLineChart;
|
||||
PieChart mPieChart;
|
||||
DrawPieChart mDrawPieChart;
|
||||
DrawLineChart mDrawLineChart;
|
||||
|
||||
private int idx;
|
||||
private int max;
|
||||
private int month;
|
||||
private int day;
|
||||
|
||||
private boolean left_invisible;
|
||||
private boolean right_invisible;
|
||||
|
||||
private String date;
|
||||
|
||||
private List<RecordBean> records;
|
||||
private RecordBean mRecord;
|
||||
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);//设置布局文件
|
||||
//初始化视图部件
|
||||
setContentView(R.layout.record_details);
|
||||
mLineChart = findViewById(R.id.lineChart);
|
||||
mPieChart = findViewById(R.id.mPiechart);
|
||||
btn_left = findViewById(R.id.left);
|
||||
btn_right = findViewById(R.id.right);
|
||||
mDate = findViewById(R.id.date);
|
||||
mStartTime = findViewById(R.id.startTime);
|
||||
mStopTime = findViewById(R.id.stopTime);
|
||||
mSleepTime = findViewById(R.id.sleepTime);
|
||||
mDeep = findViewById(R.id.deep);
|
||||
mSwallow = findViewById(R.id.swallow);
|
||||
mDream = findViewById(R.id.dream);
|
||||
|
||||
idx = this.getIntent().getIntExtra("position", 0);
|
||||
date = this.getIntent().getStringExtra("date");
|
||||
readLog();//读取睡眠记录
|
||||
setText();//设置文本内容
|
||||
initView();//初始化视图
|
||||
}
|
||||
//视图
|
||||
private void initView()
|
||||
{
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx >= max) {
|
||||
right_invisible = true;
|
||||
btn_right.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (idx == 0) {
|
||||
left_invisible = true;
|
||||
btn_left.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
//设置文本
|
||||
private void setText() {
|
||||
mDate.setText(String.format(Locale.getDefault(), "%d月%d日", month, day));
|
||||
mStartTime.setText(String.format(getResources().getString(R.string.sleep_time), mRecord.getStartTime()));
|
||||
mStopTime.setText(String.format(getResources().getString(R.string.get_up_time), mRecord.getEndTime()));
|
||||
mSleepTime.setText(String.format(Locale.getDefault(), "时长 %02d:%02d",
|
||||
mRecord.getTotalTime() / 60, mRecord.getTotalTime() % 60));
|
||||
mDeep.setText(String.format(Locale.getDefault(), "深度睡眠 %02d:%02d",
|
||||
mRecord.getDeepTime() / 60, mRecord.getDeepTime() % 60));
|
||||
mSwallow.setText(String.format(Locale.getDefault(), "浅层睡眠 %02d:%02d",
|
||||
mRecord.getSwallowTime() / 60, mRecord.getSwallowTime() % 60));
|
||||
mDream.setText(String.format(Locale.getDefault(), "醒/梦 %02d:%02d",
|
||||
mRecord.getAwakeTime() / 60, mRecord.getAwakeTime() % 60));
|
||||
}
|
||||
|
||||
//左上的返回
|
||||
public void ClickBackDetails(View v) {
|
||||
SleepresultScreen.this.finish();
|
||||
}
|
||||
|
||||
//向左切换睡眠记录
|
||||
public void ClickLeft(View v) {
|
||||
mRecord = records.get(--idx);
|
||||
setText();
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx == 0) {
|
||||
left_invisible = true;
|
||||
btn_left.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (right_invisible) {
|
||||
right_invisible = false;
|
||||
btn_right.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
//向右切换睡眠记录
|
||||
public void ClickRight(View v) {
|
||||
mRecord = records.get(++idx);
|
||||
setText();
|
||||
mDrawPieChart = new DrawPieChart(mPieChart, mRecord, getResources());
|
||||
mDrawLineChart = new DrawLineChart(mLineChart, mRecord, getResources());
|
||||
if (idx >= max) {
|
||||
right_invisible = true;
|
||||
btn_right.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
if (left_invisible) {
|
||||
left_invisible = false;
|
||||
btn_left.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取睡眠记录
|
||||
*/
|
||||
private void readLog() {
|
||||
GetRecord mGetRecord = getRecord();
|
||||
records = mGetRecord.queryByDate(date);
|
||||
mRecord = records.get(idx);
|
||||
max = records.size() - 1;
|
||||
String[] arr = mRecord.getDate().split("-");
|
||||
month = Integer.parseInt(arr[0]);
|
||||
day = Integer.parseInt(arr[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.example.sleep;
|
||||
|
||||
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.database.MyDataBaseHelper;
|
||||
import com.example.sleep.drawChart.SimpleLineChart;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SuggestScreen extends Fragment {
|
||||
private RecordBean mRecord;//睡眠记录对象
|
||||
|
||||
private SimpleLineChart mSimpleLineChart;//折线图组件
|
||||
private TextView suggestion, gradeText;//文本显示组件
|
||||
//视图
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.chart_fragment, container, false); // 加载布局文件
|
||||
init(view);//初始化方法
|
||||
return view;//返回视图
|
||||
}
|
||||
|
||||
public void init(View view) {
|
||||
suggestion = (TextView) view.findViewById(R.id.suggest);//建议文本
|
||||
gradeText = (TextView) view.findViewById(R.id.grade);//评分文本
|
||||
//调用数据库
|
||||
MyDataBaseHelper dbHelp = new MyDataBaseHelper(getActivity());
|
||||
SQLiteDatabase sqLiteDatabase = dbHelp.getWritableDatabase();
|
||||
try {
|
||||
Cursor cursor = sqLiteDatabase.rawQuery("select * from grade_table", null);//从数据库中查询数据
|
||||
|
||||
int counts = Math.min(cursor.getCount(), 7);
|
||||
String[] time = new String[counts];//存储时间的数组
|
||||
double[] grade = new double[counts];//存储评分的数组
|
||||
if (cursor.moveToLast() == true) {
|
||||
for (int i = 0; i < counts; i++) {
|
||||
time[i] = cursor.getString(cursor.getColumnIndex("time"));//获取时间并存储到数组中
|
||||
grade[i] = cursor.getDouble(cursor.getColumnIndex("grade"));//获取评分并存储到数组中
|
||||
cursor.moveToPrevious();//移动到上一行
|
||||
}
|
||||
}
|
||||
if (time.length != 0) {
|
||||
mSimpleLineChart = (SimpleLineChart) view.findViewById(R.id.simpleLineChart);//获取折线图组件
|
||||
String[] yItem = {"100", "80", "60", "40", "20", "0"};
|
||||
mSimpleLineChart.setXItem(time);//设置x轴刻度标签
|
||||
mSimpleLineChart.setYItem(yItem);//设置y周刻度标签
|
||||
HashMap<Integer, Double> pointMap = new HashMap();//存储折线点的hashmap
|
||||
for (int i = 0; i < time.length; i++) {
|
||||
pointMap.put(i, grade[i] / 100);//将时间和评分的映射存储到hashmap中
|
||||
}
|
||||
mSimpleLineChart.setData(pointMap);//设置折线图的数据
|
||||
SharedPreferences sharedpref = getActivity().getSharedPreferences("info", MODE_PRIVATE);
|
||||
String suggest = sharedpref.getString("suggestion", "");//建议文本
|
||||
float gra = sharedpref.getFloat("grade", 0);//评分
|
||||
suggestion.setText("睡眠助手的建议:\n"+ suggest);
|
||||
long x = Math.round(gra);
|
||||
gradeText.setText(x + "");
|
||||
} else {
|
||||
suggestion.setText("睡眠助手的建议:\n体验一下我们的app吧~");//时间组为空时,显示
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.i("e", e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void getSleepTime(){
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.example.sleep.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.sleep.App;
|
||||
import com.example.sleep.R;
|
||||
import com.example.sleep.database.UserBean;
|
||||
import com.example.sleep.database.dao.DaoSession;
|
||||
import com.example.sleep.database.dao.UserBeanDao;
|
||||
//处理用户的注册操作
|
||||
public class RegisterActivity extends AppCompatActivity {
|
||||
|
||||
private EditText registerUsernameEditText;//用户名
|
||||
private EditText registerPasswordEditText;//密码
|
||||
private Button registerButton;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
//设置一些基本的视图元素,并为其中视图元素设置一个简单的事件处理程序。
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_register);
|
||||
|
||||
registerUsernameEditText = findViewById(R.id.usernameEditText);
|
||||
registerPasswordEditText = findViewById(R.id.passwordEditText);
|
||||
registerButton = findViewById(R.id.registerButton);
|
||||
findViewById(R.id.btn_AfterSleep).setOnClickListener(v -> finish());
|
||||
|
||||
registerButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
// 处理注册逻辑
|
||||
String newUsername = registerUsernameEditText.getText().toString();
|
||||
String newPassword = registerPasswordEditText.getText().toString();
|
||||
// 将新用户的用户名和密码保存到GreenDAO数据库中
|
||||
// 执行注册操作
|
||||
// 创建一个新的 User 对象并设置用户名和密码
|
||||
UserBean newUser = new UserBean();
|
||||
newUser.setUsername(newUsername);
|
||||
newUser.setPassword(newPassword);
|
||||
// 获取 GreenDAO 的 DAOSession
|
||||
DaoSession mDaoSession = App.mDaoSession;
|
||||
UserBeanDao userBeanDao = mDaoSession.getUserBeanDao();
|
||||
long id = userBeanDao.insert(newUser);
|
||||
System.out.println("userDao: " + id);
|
||||
if (id != -1) Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.example.sleep.calendarInfo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.sleep.R;
|
||||
import com.ldf.calendar.Utils;
|
||||
import com.ldf.calendar.component.State;
|
||||
import com.ldf.calendar.interf.IDayRenderer;
|
||||
import com.ldf.calendar.model.CalendarDate;
|
||||
import com.ldf.calendar.view.DayView;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CustomDayView extends DayView {
|
||||
|
||||
private final CalendarDate today = new CalendarDate();//当前日期
|
||||
private TextView dateTv;//日期文本显示
|
||||
private ImageView marker;//标记图标
|
||||
private View selectedBackground;//选中背景
|
||||
private View todayBackground;//今天的背景
|
||||
|
||||
|
||||
public CustomDayView(Context context, int layoutResource) {
|
||||
super(context, layoutResource);
|
||||
dateTv = findViewById(R.id.date);//找到日期文本视图
|
||||
marker = findViewById(R.id.maker);// 找到标记图标视图
|
||||
selectedBackground = findViewById(R.id.selected_background); // 找到选中背景视图
|
||||
todayBackground = findViewById(R.id.today_background); // 找到今天的背景视图
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshContent() {
|
||||
super.refreshContent();// 刷新日历内容
|
||||
renderToday(day.getDate()); // 渲染今天的日期
|
||||
renderSelect(day.getState());// 渲染选中状态
|
||||
renderMarker(day.getDate(), day.getState());// 渲染标记
|
||||
dateTv.setTextSize(19);// 设置日期文本大小
|
||||
super.refreshContent();// 再次刷新日历内容
|
||||
}
|
||||
|
||||
// 渲染标记的显示
|
||||
private void renderMarker(CalendarDate date, State state) {
|
||||
if (Utils.loadMarkData().containsKey(date.toString())) {
|
||||
if (state == State.SELECT || date.toString().equals(today.toString())) {
|
||||
marker.setVisibility(GONE);
|
||||
} else {
|
||||
marker.setVisibility(VISIBLE);
|
||||
if (Objects.equals(Utils.loadMarkData().get(date.toString()), "0")) {
|
||||
marker.setEnabled(true);
|
||||
} else {
|
||||
marker.setEnabled(false);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
marker.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染选中状态
|
||||
private void renderSelect(State state) {
|
||||
if (state == State.SELECT) {
|
||||
selectedBackground.setVisibility(View.VISIBLE);
|
||||
dateTv.setTextColor(Color.BLACK);
|
||||
} else if (state == State.NEXT_MONTH || state == State.PAST_MONTH) {
|
||||
selectedBackground.setVisibility(GONE);
|
||||
dateTv.setTextColor(Color.parseColor("#3666a2"));
|
||||
} else {
|
||||
selectedBackground.setVisibility(GONE);
|
||||
dateTv.setTextColor(Color.WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染今天的日期显示
|
||||
private void renderToday(CalendarDate date) {
|
||||
if (date != null) {
|
||||
if (date.equals(today)) {
|
||||
dateTv.setText("今");
|
||||
dateTv.setTextColor(Color.BLACK);
|
||||
//todayBackground.setVisibility(VISIBLE);
|
||||
} else {
|
||||
dateTv.setText(date.day + "");
|
||||
todayBackground.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
// 复制当前的日历视图
|
||||
public IDayRenderer copy() {
|
||||
return new CustomDayView(context, layoutResource);
|
||||
}
|
||||
}
|
@ -0,0 +1,198 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.sleep.App;
|
||||
import com.example.sleep.database.dao.DaoSession;
|
||||
import com.example.sleep.database.dao.RecordBeanDao;
|
||||
import com.example.sleep.database.dao.RemindBeanDao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 处理睡眠记录数据库
|
||||
*/
|
||||
public class GetRecord {
|
||||
private RecordBeanDao recordBeanDao;
|
||||
private RemindBeanDao remindBeanDao;
|
||||
private static final String TAG = "GreenDao";
|
||||
private static GetRecord getRecord;
|
||||
|
||||
private GetRecord() {
|
||||
DaoSession mDaoSession = App.mDaoSession;
|
||||
recordBeanDao = mDaoSession.getRecordBeanDao();
|
||||
remindBeanDao = mDaoSession.getRemindBeanDao();
|
||||
}
|
||||
|
||||
public static GetRecord getRecord() {
|
||||
if (getRecord == null) {
|
||||
getRecord = new GetRecord();
|
||||
}
|
||||
return getRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入睡眠记录
|
||||
*
|
||||
* @param date 日期
|
||||
* @param startTime 开始时间
|
||||
* @return 睡眠记录
|
||||
*/
|
||||
public RecordBean insertData(String date, String startTime) {
|
||||
RecordBean mRecord = new RecordBean(null, date, startTime, startTime, 0,
|
||||
false, 0, 0, 0, "", false);
|
||||
try {
|
||||
recordBeanDao.insert(mRecord);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库插入失败");
|
||||
}
|
||||
return mRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id删除睡眠记录
|
||||
*
|
||||
* @param id 记录id
|
||||
*/
|
||||
public void deleteById(Long id) {
|
||||
try {
|
||||
recordBeanDao.deleteByKey(id);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据记录删除
|
||||
*
|
||||
* @param mRecord 记录对象
|
||||
*/
|
||||
public void delete(RecordBean mRecord) {
|
||||
try {
|
||||
recordBeanDao.delete(mRecord);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "数据库删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录
|
||||
*
|
||||
* @param mRecord 记录对象
|
||||
* @param sleepDetail 要新增的信息
|
||||
*/
|
||||
public void update(RecordBean mRecord, String sleepDetail) {
|
||||
if (mRecord != null) {
|
||||
mRecord.setSleepDetail(mRecord.getSleepDetail() + sleepDetail);
|
||||
recordBeanDao.update(mRecord);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成一次睡眠记录时的最终更新
|
||||
*
|
||||
* @param mRecord 睡眠记录对象
|
||||
* @param endHour 结束时间
|
||||
* @param endMin 结束时间
|
||||
* @param totalTime 总时间
|
||||
* @param deepTime 深度睡眠时间
|
||||
* @param swallowTime 浅层睡眠时间
|
||||
* @param awakeTime 醒的时间
|
||||
*/
|
||||
public void finalUpdate(RecordBean mRecord, int endHour, int endMin, long totalTime,
|
||||
int deepTime, int swallowTime, int awakeTime) {
|
||||
totalTime /= 1000 * 60;
|
||||
if (totalTime > 2) {
|
||||
mRecord.setDrawChart(true);
|
||||
}
|
||||
mRecord.setEndTime(String.format(Locale.getDefault(), "%02d:%02d", endHour, endMin));
|
||||
mRecord.setTotalTime((int) totalTime);
|
||||
mRecord.setDeepTime(deepTime);
|
||||
mRecord.setSwallowTime(swallowTime);
|
||||
mRecord.setAwakeTime(awakeTime);
|
||||
mRecord.setValid(true);
|
||||
recordBeanDao.update(mRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有记录
|
||||
*
|
||||
* @return 记录对象的列表
|
||||
*/
|
||||
public List queryAllList() {
|
||||
return recordBeanDao.queryBuilder().orderDesc(RecordBeanDao.Properties.Id).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按日期查找
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 记录的对象
|
||||
*/
|
||||
public List queryByDate(String date) {
|
||||
return recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Date.eq(date)).orderAsc(RecordBeanDao.Properties.Id).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按月查找
|
||||
*
|
||||
* @param month 月份
|
||||
* @param days 对应的月有多少天
|
||||
* @return 每天是否有记录
|
||||
*/
|
||||
public boolean[] queryByMonth(String month, int days) {
|
||||
boolean[] result = new boolean[days + 1];
|
||||
String date;
|
||||
for (int i = 1; i <= days; ++i) {
|
||||
date = month + "-" + i;
|
||||
result[i] = !recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Date.eq(date)).build().list().isEmpty();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id获取记录
|
||||
*/
|
||||
public RecordBean getRecordById(long id) {
|
||||
return recordBeanDao.queryBuilder().where(RecordBeanDao.Properties.Id.eq(id)).build().unique();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得最新的记录
|
||||
*/
|
||||
public RecordBean getLatestRecord() {
|
||||
List<RecordBean> records = recordBeanDao.queryBuilder().orderDesc(RecordBeanDao.Properties.Id).list();
|
||||
if (!records.isEmpty())
|
||||
return records.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改提醒时间
|
||||
*
|
||||
* @param time 时间
|
||||
*/
|
||||
public void updateRemind(String time) {
|
||||
if (remindBeanDao.queryBuilder().list().isEmpty()) {
|
||||
remindBeanDao.insert(new RemindBean(null, "22:00"));
|
||||
}
|
||||
RemindBean remindBean = remindBeanDao.queryBuilder().list().get(0);
|
||||
if (remindBean != null) {
|
||||
remindBean.setTime(time);
|
||||
remindBeanDao.update(remindBean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提醒时间
|
||||
*
|
||||
* @return 提醒的时间
|
||||
*/
|
||||
public String getRemind() {
|
||||
if (remindBeanDao.queryBuilder().list().isEmpty()) {
|
||||
remindBeanDao.insert(new RemindBean(null, "22:00"));
|
||||
}
|
||||
return remindBeanDao.queryBuilder().list().get(0).getTime();
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
public class MyDataBaseHelper extends SQLiteOpenHelper//用于创建和更新数据库
|
||||
{
|
||||
private static final String DB_NAME = "DB2.db";
|
||||
private static final String TABLE_NAME = "grade_table";
|
||||
private static final int DB_VERSION = 1;
|
||||
|
||||
|
||||
// 用户状态表
|
||||
public static final String TABLE_STATUS = "statusTable";
|
||||
public static final String STATUS_ID = "status_id";
|
||||
public static final String USER_NAME = "user_name";
|
||||
public static final String CONTENT = "content";
|
||||
public static final String IMAGE_URL = "image_url";
|
||||
public static final String TIMESTAMP = "timestamp";
|
||||
|
||||
// 评论表
|
||||
public static final String TABLE_COMMENTS = "commentsTable";
|
||||
public static final String COMMENT_ID = "comment_id";
|
||||
public static final String COMMENTER = "commenter";
|
||||
public static final String COMMENT = "comment";
|
||||
|
||||
public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
|
||||
super(context, name, factory, version);
|
||||
}
|
||||
|
||||
public MyDataBaseHelper(Context context) {
|
||||
this(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
try {
|
||||
//创建成绩表
|
||||
String CREATE_TABLE = "CREATE TABLE if not exists " + TABLE_NAME
|
||||
+ "(_id INTEGER PRIMARY KEY autoincrement,time TEXT," +
|
||||
"sumOfSleep INTEGER," +
|
||||
"timeOfSleep DOUBLE,grade DOUBLE)";
|
||||
db.execSQL(CREATE_TABLE);
|
||||
//创建用户状态表
|
||||
String createStatusTable = "CREATE TABLE " + TABLE_STATUS + "(" +
|
||||
STATUS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
USER_NAME + " TEXT," +
|
||||
CONTENT + " TEXT," +
|
||||
IMAGE_URL + " TEXT," +
|
||||
TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP)";
|
||||
//创建评论表
|
||||
String createCommentsTable = "CREATE TABLE " + TABLE_COMMENTS + "(" +
|
||||
COMMENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
STATUS_ID + " INTEGER," +
|
||||
COMMENTER + " TEXT," +
|
||||
COMMENT + " TEXT," +
|
||||
TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
|
||||
"FOREIGN KEY(" + STATUS_ID + ") REFERENCES " + TABLE_STATUS + "(" + STATUS_ID + "))";
|
||||
|
||||
db.execSQL(createStatusTable);//执行创建用户状态表的SQL语句
|
||||
db.execSQL(createCommentsTable);//执行创建评论表的SQL语句
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.i("e", e.toString());//若出现异常,记录错误日志
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
System.out.println("database update!");//输出日志,表示数据库正在更新
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATUS);//删除用户状态表
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);//删除评论表
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);//删除成绩表
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import org.greenrobot.greendao.annotation.Entity;
|
||||
import org.greenrobot.greendao.annotation.Generated;
|
||||
import org.greenrobot.greendao.annotation.Id;
|
||||
//声明实体类
|
||||
@Entity
|
||||
public class RemindBean
|
||||
{
|
||||
@Id(autoincrement = true)
|
||||
private Long id;//主键
|
||||
private String time;//time
|
||||
|
||||
@Generated(hash = 1395260710)
|
||||
public RemindBean(Long id, String time) {
|
||||
this.id = id;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Generated(hash = 1914622572)
|
||||
public RemindBean() {
|
||||
}
|
||||
//对上述设置的值进行set和get方法
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return this.time;
|
||||
}
|
||||
|
||||
public void setTime(String time) {
|
||||
this.time = time;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.example.sleep.database;
|
||||
|
||||
import org.greenrobot.greendao.annotation.Entity;
|
||||
import org.greenrobot.greendao.annotation.Generated;
|
||||
import org.greenrobot.greendao.annotation.Id;
|
||||
//声明实体类
|
||||
@Entity
|
||||
public class UserBean {
|
||||
@Id(autoincrement = true)
|
||||
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
@Generated(hash = 2052951463)
|
||||
public UserBean(Long id, String username, String password) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Generated(hash = 1203313951)
|
||||
public UserBean() {
|
||||
}
|
||||
//对设置的值进行set和get操作
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||
import android.util.Log;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDaoMaster;
|
||||
import org.greenrobot.greendao.database.StandardDatabase;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseOpenHelper;
|
||||
import org.greenrobot.greendao.identityscope.IdentityScopeType;
|
||||
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* Master of DAO (schema version 2): knows all DAOs.
|
||||
*/
|
||||
public class DaoMaster extends AbstractDaoMaster {
|
||||
public static final int SCHEMA_VERSION = 2;
|
||||
|
||||
/** Creates underlying database table using DAOs. */
|
||||
public static void createAllTables(Database db, boolean ifNotExists) {
|
||||
RecordBeanDao.createTable(db, ifNotExists);
|
||||
RemindBeanDao.createTable(db, ifNotExists);
|
||||
UserBeanDao.createTable(db, ifNotExists);
|
||||
}
|
||||
|
||||
/** Drops underlying database table using DAOs. */
|
||||
public static void dropAllTables(Database db, boolean ifExists) {
|
||||
RecordBeanDao.dropTable(db, ifExists);
|
||||
RemindBeanDao.dropTable(db, ifExists);
|
||||
UserBeanDao.dropTable(db, ifExists);
|
||||
}
|
||||
|
||||
/**
|
||||
* WARNING: Drops all table on Upgrade! Use only during development.
|
||||
* Convenience method using a {@link DevOpenHelper}.
|
||||
*/
|
||||
public static DaoSession newDevSession(Context context, String name) {
|
||||
Database db = new DevOpenHelper(context, name).getWritableDb();
|
||||
DaoMaster daoMaster = new DaoMaster(db);
|
||||
return daoMaster.newSession();
|
||||
}
|
||||
|
||||
public DaoMaster(SQLiteDatabase db) {
|
||||
this(new StandardDatabase(db));
|
||||
}
|
||||
|
||||
public DaoMaster(Database db) {
|
||||
super(db, SCHEMA_VERSION);
|
||||
registerDaoClass(RecordBeanDao.class);
|
||||
registerDaoClass(RemindBeanDao.class);
|
||||
registerDaoClass(UserBeanDao.class);
|
||||
}
|
||||
|
||||
public DaoSession newSession() {
|
||||
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
|
||||
}
|
||||
|
||||
public DaoSession newSession(IdentityScopeType type) {
|
||||
return new DaoSession(db, type, daoConfigMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
|
||||
*/
|
||||
public static abstract class OpenHelper extends DatabaseOpenHelper {
|
||||
public OpenHelper(Context context, String name) {
|
||||
super(context, name, SCHEMA_VERSION);
|
||||
}
|
||||
|
||||
public OpenHelper(Context context, String name, CursorFactory factory) {
|
||||
super(context, name, factory, SCHEMA_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Database db) {
|
||||
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
|
||||
createAllTables(db, false);
|
||||
}
|
||||
}
|
||||
|
||||
/** WARNING: Drops all table on Upgrade! Use only during development. */
|
||||
public static class DevOpenHelper extends OpenHelper {
|
||||
public DevOpenHelper(Context context, String name) {
|
||||
super(context, name);
|
||||
}
|
||||
|
||||
public DevOpenHelper(Context context, String name, CursorFactory factory) {
|
||||
super(context, name, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(Database db, int oldVersion, int newVersion) {
|
||||
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
|
||||
dropAllTables(db, true);
|
||||
onCreate(db);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.AbstractDaoSession;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.identityscope.IdentityScopeType;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
import com.example.sleep.database.RemindBean;
|
||||
import com.example.sleep.database.UserBean;
|
||||
|
||||
import com.example.sleep.database.dao.RecordBeanDao;
|
||||
import com.example.sleep.database.dao.RemindBeanDao;
|
||||
import com.example.sleep.database.dao.UserBeanDao;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see org.greenrobot.greendao.AbstractDaoSession
|
||||
*/
|
||||
public class DaoSession extends AbstractDaoSession {
|
||||
|
||||
private final DaoConfig recordBeanDaoConfig;
|
||||
private final DaoConfig remindBeanDaoConfig;
|
||||
private final DaoConfig userBeanDaoConfig;
|
||||
|
||||
private final RecordBeanDao recordBeanDao;
|
||||
private final RemindBeanDao remindBeanDao;
|
||||
private final UserBeanDao userBeanDao;
|
||||
|
||||
public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
|
||||
daoConfigMap) {
|
||||
super(db);
|
||||
|
||||
recordBeanDaoConfig = daoConfigMap.get(RecordBeanDao.class).clone();
|
||||
recordBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
remindBeanDaoConfig = daoConfigMap.get(RemindBeanDao.class).clone();
|
||||
remindBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
userBeanDaoConfig = daoConfigMap.get(UserBeanDao.class).clone();
|
||||
userBeanDaoConfig.initIdentityScope(type);
|
||||
|
||||
recordBeanDao = new RecordBeanDao(recordBeanDaoConfig, this);
|
||||
remindBeanDao = new RemindBeanDao(remindBeanDaoConfig, this);
|
||||
userBeanDao = new UserBeanDao(userBeanDaoConfig, this);
|
||||
|
||||
registerDao(RecordBean.class, recordBeanDao);
|
||||
registerDao(RemindBean.class, remindBeanDao);
|
||||
registerDao(UserBean.class, userBeanDao);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
recordBeanDaoConfig.clearIdentityScope();
|
||||
remindBeanDaoConfig.clearIdentityScope();
|
||||
userBeanDaoConfig.clearIdentityScope();
|
||||
}
|
||||
|
||||
public RecordBeanDao getRecordBeanDao() {
|
||||
return recordBeanDao;
|
||||
}
|
||||
|
||||
public RemindBeanDao getRemindBeanDao() {
|
||||
return remindBeanDao;
|
||||
}
|
||||
|
||||
public UserBeanDao getUserBeanDao() {
|
||||
return userBeanDao;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import static com.example.sleep.database.MyDataBaseHelper.CONTENT;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.IMAGE_URL;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.TIMESTAMP;
|
||||
import static com.example.sleep.database.MyDataBaseHelper.USER_NAME;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.example.sleep.ItemModel;
|
||||
import com.example.sleep.database.MyDataBaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseManager {
|
||||
private MyDataBaseHelper dbHelper;
|
||||
private SQLiteDatabase database;
|
||||
|
||||
public DatabaseManager(Context context) {
|
||||
dbHelper = new MyDataBaseHelper(context);
|
||||
}
|
||||
|
||||
public void open() {
|
||||
database = dbHelper.getWritableDatabase();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
dbHelper.close();
|
||||
}
|
||||
|
||||
// A用户发表心情
|
||||
public long postMood(String userName, String content, String imageUrl) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(USER_NAME, userName);
|
||||
values.put(CONTENT, content);
|
||||
values.put(IMAGE_URL, imageUrl);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
|
||||
}
|
||||
|
||||
public List<ItemModel> getUserNameEvent(String userName) {
|
||||
// 使用参数化查询以防止SQL注入
|
||||
String selection = USER_NAME + " = ?";
|
||||
String[] selectionArgs = {userName};
|
||||
List<ItemModel> dataList = new ArrayList<>();
|
||||
Cursor cursor = database.query(MyDataBaseHelper.TABLE_STATUS, null, selection, selectionArgs, null, null, TIMESTAMP + " DESC");
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
String text = cursor.getString(cursor.getColumnIndex(CONTENT));
|
||||
String imageUrl = cursor.getString(cursor.getColumnIndex(IMAGE_URL));
|
||||
|
||||
// 根据你的ItemModel的构造函数来适当地修改下一行
|
||||
dataList.add(new ItemModel(imageUrl, text));
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
database.close();
|
||||
return dataList;
|
||||
}
|
||||
|
||||
// B用户评论A用户的心情
|
||||
public long commentMood(long statusId, String commenter, String comment) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MyDataBaseHelper.STATUS_ID, statusId);
|
||||
values.put(MyDataBaseHelper.COMMENTER, commenter);
|
||||
values.put(MyDataBaseHelper.COMMENT, comment);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
|
||||
}
|
||||
|
||||
// B用户查看A用户的心情和评论
|
||||
public Cursor viewMoodAndComments(long statusId) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{String.valueOf(statusId)});
|
||||
}
|
||||
|
||||
// A用户查看自己的心情和B用户的评论
|
||||
public Cursor viewOwnMoodAndComments(String userName) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + USER_NAME + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{userName});
|
||||
}
|
||||
|
||||
|
||||
// 插入一条心情动态
|
||||
public long insertStatus(String userName, String content, String imageUrl) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(USER_NAME, userName);
|
||||
values.put(CONTENT, content);
|
||||
values.put(IMAGE_URL, imageUrl);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
|
||||
}
|
||||
|
||||
// 插入一条评论
|
||||
public long insertComment(long statusId, String commenter, String comment) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(MyDataBaseHelper.STATUS_ID, statusId);
|
||||
values.put(MyDataBaseHelper.COMMENTER, commenter);
|
||||
values.put(MyDataBaseHelper.COMMENT, comment);
|
||||
|
||||
return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
|
||||
}
|
||||
|
||||
public Cursor getStatusWithComments(long statusId) {
|
||||
String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
|
||||
"LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
|
||||
"ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
|
||||
" WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";
|
||||
|
||||
return database.rawQuery(query, new String[]{String.valueOf(statusId)});
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.RecordBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "RECORD_BEAN".
|
||||
*/
|
||||
public class RecordBeanDao extends AbstractDao<RecordBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "RECORD_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity RecordBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Date = new Property(1, String.class, "date", false, "DATE");
|
||||
public final static Property StartTime = new Property(2, String.class, "startTime", false, "START_TIME");
|
||||
public final static Property EndTime = new Property(3, String.class, "endTime", false, "END_TIME");
|
||||
public final static Property TotalTime = new Property(4, int.class, "totalTime", false, "TOTAL_TIME");
|
||||
public final static Property DrawChart = new Property(5, boolean.class, "drawChart", false, "DRAW_CHART");
|
||||
public final static Property DeepTime = new Property(6, int.class, "deepTime", false, "DEEP_TIME");
|
||||
public final static Property SwallowTime = new Property(7, int.class, "swallowTime", false, "SWALLOW_TIME");
|
||||
public final static Property AwakeTime = new Property(8, int.class, "awakeTime", false, "AWAKE_TIME");
|
||||
public final static Property SleepDetail = new Property(9, String.class, "sleepDetail", false, "SLEEP_DETAIL");
|
||||
public final static Property Valid = new Property(10, boolean.class, "valid", false, "VALID");
|
||||
}
|
||||
|
||||
|
||||
public RecordBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public RecordBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"RECORD_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"DATE\" TEXT," + // 1: date
|
||||
"\"START_TIME\" TEXT," + // 2: startTime
|
||||
"\"END_TIME\" TEXT," + // 3: endTime
|
||||
"\"TOTAL_TIME\" INTEGER NOT NULL ," + // 4: totalTime
|
||||
"\"DRAW_CHART\" INTEGER NOT NULL ," + // 5: drawChart
|
||||
"\"DEEP_TIME\" INTEGER NOT NULL ," + // 6: deepTime
|
||||
"\"SWALLOW_TIME\" INTEGER NOT NULL ," + // 7: swallowTime
|
||||
"\"AWAKE_TIME\" INTEGER NOT NULL ," + // 8: awakeTime
|
||||
"\"SLEEP_DETAIL\" TEXT," + // 9: sleepDetail
|
||||
"\"VALID\" INTEGER NOT NULL );"); // 10: valid
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RECORD_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, RecordBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String date = entity.getDate();
|
||||
if (date != null) {
|
||||
stmt.bindString(2, date);
|
||||
}
|
||||
|
||||
String startTime = entity.getStartTime();
|
||||
if (startTime != null) {
|
||||
stmt.bindString(3, startTime);
|
||||
}
|
||||
|
||||
String endTime = entity.getEndTime();
|
||||
if (endTime != null) {
|
||||
stmt.bindString(4, endTime);
|
||||
}
|
||||
stmt.bindLong(5, entity.getTotalTime());
|
||||
stmt.bindLong(6, entity.getDrawChart() ? 1L: 0L);
|
||||
stmt.bindLong(7, entity.getDeepTime());
|
||||
stmt.bindLong(8, entity.getSwallowTime());
|
||||
stmt.bindLong(9, entity.getAwakeTime());
|
||||
|
||||
String sleepDetail = entity.getSleepDetail();
|
||||
if (sleepDetail != null) {
|
||||
stmt.bindString(10, sleepDetail);
|
||||
}
|
||||
stmt.bindLong(11, entity.getValid() ? 1L: 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, RecordBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String date = entity.getDate();
|
||||
if (date != null) {
|
||||
stmt.bindString(2, date);
|
||||
}
|
||||
|
||||
String startTime = entity.getStartTime();
|
||||
if (startTime != null) {
|
||||
stmt.bindString(3, startTime);
|
||||
}
|
||||
|
||||
String endTime = entity.getEndTime();
|
||||
if (endTime != null) {
|
||||
stmt.bindString(4, endTime);
|
||||
}
|
||||
stmt.bindLong(5, entity.getTotalTime());
|
||||
stmt.bindLong(6, entity.getDrawChart() ? 1L: 0L);
|
||||
stmt.bindLong(7, entity.getDeepTime());
|
||||
stmt.bindLong(8, entity.getSwallowTime());
|
||||
stmt.bindLong(9, entity.getAwakeTime());
|
||||
|
||||
String sleepDetail = entity.getSleepDetail();
|
||||
if (sleepDetail != null) {
|
||||
stmt.bindString(10, sleepDetail);
|
||||
}
|
||||
stmt.bindLong(11, entity.getValid() ? 1L: 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordBean readEntity(Cursor cursor, int offset) {
|
||||
RecordBean entity = new RecordBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // date
|
||||
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // startTime
|
||||
cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // endTime
|
||||
cursor.getInt(offset + 4), // totalTime
|
||||
cursor.getShort(offset + 5) != 0, // drawChart
|
||||
cursor.getInt(offset + 6), // deepTime
|
||||
cursor.getInt(offset + 7), // swallowTime
|
||||
cursor.getInt(offset + 8), // awakeTime
|
||||
cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // sleepDetail
|
||||
cursor.getShort(offset + 10) != 0 // valid
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, RecordBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setDate(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
entity.setStartTime(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
|
||||
entity.setEndTime(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
|
||||
entity.setTotalTime(cursor.getInt(offset + 4));
|
||||
entity.setDrawChart(cursor.getShort(offset + 5) != 0);
|
||||
entity.setDeepTime(cursor.getInt(offset + 6));
|
||||
entity.setSwallowTime(cursor.getInt(offset + 7));
|
||||
entity.setAwakeTime(cursor.getInt(offset + 8));
|
||||
entity.setSleepDetail(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
|
||||
entity.setValid(cursor.getShort(offset + 10) != 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(RecordBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(RecordBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(RecordBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.RemindBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "REMIND_BEAN".
|
||||
*/
|
||||
public class RemindBeanDao extends AbstractDao<RemindBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "REMIND_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity RemindBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Time = new Property(1, String.class, "time", false, "TIME");
|
||||
}
|
||||
|
||||
|
||||
public RemindBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public RemindBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"REMIND_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"TIME\" TEXT);"); // 1: time
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"REMIND_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, RemindBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String time = entity.getTime();
|
||||
if (time != null) {
|
||||
stmt.bindString(2, time);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, RemindBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String time = entity.getTime();
|
||||
if (time != null) {
|
||||
stmt.bindString(2, time);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemindBean readEntity(Cursor cursor, int offset) {
|
||||
RemindBean entity = new RemindBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // time
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, RemindBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setTime(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(RemindBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(RemindBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(RemindBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
package com.example.sleep.database.dao;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteStatement;
|
||||
|
||||
import org.greenrobot.greendao.AbstractDao;
|
||||
import org.greenrobot.greendao.Property;
|
||||
import org.greenrobot.greendao.internal.DaoConfig;
|
||||
import org.greenrobot.greendao.database.Database;
|
||||
import org.greenrobot.greendao.database.DatabaseStatement;
|
||||
|
||||
import com.example.sleep.database.UserBean;
|
||||
|
||||
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
|
||||
/**
|
||||
* DAO for table "USER_BEAN".
|
||||
*/
|
||||
public class UserBeanDao extends AbstractDao<UserBean, Long> {
|
||||
|
||||
public static final String TABLENAME = "USER_BEAN";
|
||||
|
||||
/**
|
||||
* Properties of entity UserBean.<br/>
|
||||
* Can be used for QueryBuilder and for referencing column names.
|
||||
*/
|
||||
public static class Properties {
|
||||
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
|
||||
public final static Property Username = new Property(1, String.class, "username", false, "USERNAME");
|
||||
public final static Property Password = new Property(2, String.class, "password", false, "PASSWORD");
|
||||
}
|
||||
|
||||
|
||||
public UserBeanDao(DaoConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
public UserBeanDao(DaoConfig config, DaoSession daoSession) {
|
||||
super(config, daoSession);
|
||||
}
|
||||
|
||||
/** Creates the underlying database table. */
|
||||
public static void createTable(Database db, boolean ifNotExists) {
|
||||
String constraint = ifNotExists? "IF NOT EXISTS ": "";
|
||||
db.execSQL("CREATE TABLE " + constraint + "\"USER_BEAN\" (" + //
|
||||
"\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
|
||||
"\"USERNAME\" TEXT," + // 1: username
|
||||
"\"PASSWORD\" TEXT);"); // 2: password
|
||||
}
|
||||
|
||||
/** Drops the underlying database table. */
|
||||
public static void dropTable(Database db, boolean ifExists) {
|
||||
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER_BEAN\"";
|
||||
db.execSQL(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(DatabaseStatement stmt, UserBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String username = entity.getUsername();
|
||||
if (username != null) {
|
||||
stmt.bindString(2, username);
|
||||
}
|
||||
|
||||
String password = entity.getPassword();
|
||||
if (password != null) {
|
||||
stmt.bindString(3, password);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void bindValues(SQLiteStatement stmt, UserBean entity) {
|
||||
stmt.clearBindings();
|
||||
|
||||
Long id = entity.getId();
|
||||
if (id != null) {
|
||||
stmt.bindLong(1, id);
|
||||
}
|
||||
|
||||
String username = entity.getUsername();
|
||||
if (username != null) {
|
||||
stmt.bindString(2, username);
|
||||
}
|
||||
|
||||
String password = entity.getPassword();
|
||||
if (password != null) {
|
||||
stmt.bindString(3, password);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long readKey(Cursor cursor, int offset) {
|
||||
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserBean readEntity(Cursor cursor, int offset) {
|
||||
UserBean entity = new UserBean( //
|
||||
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
|
||||
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // username
|
||||
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // password
|
||||
);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readEntity(Cursor cursor, UserBean entity, int offset) {
|
||||
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
|
||||
entity.setUsername(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
|
||||
entity.setPassword(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Long updateKeyAfterInsert(UserBean entity, long rowId) {
|
||||
entity.setId(rowId);
|
||||
return rowId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getKey(UserBean entity) {
|
||||
if(entity != null) {
|
||||
return entity.getId();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasKey(UserBean entity) {
|
||||
return entity.getId() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean isEntityUpdateable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package com.example.sleep.drawChart;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
|
||||
import com.github.mikephil.charting.charts.LineChart;
|
||||
import com.github.mikephil.charting.components.AxisBase;
|
||||
import com.github.mikephil.charting.components.Legend;
|
||||
import com.github.mikephil.charting.components.LimitLine;
|
||||
import com.github.mikephil.charting.components.XAxis;
|
||||
import com.github.mikephil.charting.components.YAxis;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.data.LineData;
|
||||
import com.github.mikephil.charting.data.LineDataSet;
|
||||
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
|
||||
import com.example.sleep.R;
|
||||
import com.example.sleep.database.RecordBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DrawLineChart {
|
||||
private LineChart mLineChart;
|
||||
private RecordBean mRecord;
|
||||
private Resources mResources;
|
||||
private ArrayList<Integer> times;
|
||||
private ArrayList<Float> sleepDetails;
|
||||
private List<Entry> entries = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 绘制折线图
|
||||
*
|
||||
* @param lineChart 折线图元素
|
||||
* @param mRecord 睡眠记录
|
||||
* @param mResources 绘图资源
|
||||
*/
|
||||
public DrawLineChart(LineChart lineChart, RecordBean mRecord, Resources mResources) {
|
||||
mLineChart = lineChart;
|
||||
mLineChart.setDrawBorders(false);
|
||||
mLineChart.setNoDataText("睡眠时间太短啦!没有足够数据!");
|
||||
mLineChart.setNoDataTextColor(Color.WHITE);
|
||||
mLineChart.setDrawGridBackground(true);
|
||||
mLineChart.setGridBackgroundColor(mResources.getColor(R.color.transparent_gray));
|
||||
mLineChart.setDragEnabled(true);
|
||||
mLineChart.animateX(1000);
|
||||
mLineChart.setScaleEnabled(true);
|
||||
mLineChart.setPinchZoom(true);
|
||||
mLineChart.getDescription().setEnabled(false);
|
||||
//画折线图
|
||||
if (mRecord.getDrawChart()) {
|
||||
this.mResources = mResources;
|
||||
this.mRecord = mRecord;
|
||||
readRecordDetails();
|
||||
drawChart();
|
||||
} else {
|
||||
mLineChart.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void setRecord(RecordBean mRecord) {
|
||||
this.mRecord = mRecord;
|
||||
if (mRecord.getDrawChart()) {
|
||||
readRecordDetails();
|
||||
drawChart();
|
||||
} else {
|
||||
mLineChart.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void readRecordDetails() {
|
||||
String[] arr = mRecord.getSleepDetail().split(" ");
|
||||
String[] buf;
|
||||
for (String e : arr) {
|
||||
buf = e.split(",");
|
||||
entries.add(new Entry(Integer.parseInt(buf[0]), Float.parseFloat(buf[1])));
|
||||
}
|
||||
}
|
||||
|
||||
private void drawChart() {
|
||||
// 创建线性数据集并设置属性
|
||||
LineDataSet lineDataSet = new LineDataSet(entries, "");
|
||||
lineDataSet.setColor(mResources.getColor(R.color.Pie_Yellow));
|
||||
lineDataSet.setLineWidth(1.6f);
|
||||
lineDataSet.setDrawCircles(false);
|
||||
lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
|
||||
|
||||
// 设置线性图数据并绘制
|
||||
LineData data = new LineData(lineDataSet);
|
||||
data.setDrawValues(false);
|
||||
|
||||
// 设置X轴属性
|
||||
XAxis xAxis = mLineChart.getXAxis();
|
||||
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
|
||||
xAxis.setGranularity(1f);
|
||||
xAxis.setLabelCount(8, false);
|
||||
xAxis.setTextColor(Color.WHITE);
|
||||
xAxis.setDrawGridLines(false);
|
||||
xAxis.setLabelRotationAngle(0);
|
||||
xAxis.setValueFormatter(new IAxisValueFormatter() {
|
||||
@Override
|
||||
public String getFormattedValue(float value, AxisBase axis) {
|
||||
int IValue = (int) value;
|
||||
return String.format(Locale.getDefault(), "%02d:%02d", IValue % 1440 / 60, IValue % 60);
|
||||
}
|
||||
});
|
||||
|
||||
// 设置左侧Y轴属性
|
||||
YAxis leftYAxis = mLineChart.getAxisLeft();
|
||||
leftYAxis.setEnabled(true);
|
||||
leftYAxis.setDrawGridLines(false);
|
||||
leftYAxis.setAxisMinimum(0);
|
||||
leftYAxis.setValueFormatter(new IAxisValueFormatter() {
|
||||
@Override
|
||||
public String getFormattedValue(float value, AxisBase axis) {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
// 设置限制线并添加到左侧Y轴
|
||||
LimitLine limitLine = new LimitLine(0.37f, "深度睡眠");
|
||||
limitLine.setLineColor(mResources.getColor(R.color.Pie_Green));
|
||||
limitLine.setTextColor(mResources.getColor(R.color.Pie_Green));
|
||||
leftYAxis.addLimitLine(limitLine);
|
||||
|
||||
LimitLine limitLine1 = new LimitLine(0.8f, "浅层睡眠");
|
||||
limitLine1.setLineColor(mResources.getColor(R.color.Pie_Blue));
|
||||
limitLine1.setTextColor(mResources.getColor(R.color.Pie_Blue));
|
||||
leftYAxis.addLimitLine(limitLine1);
|
||||
|
||||
// 设置图例并绘制线性图
|
||||
Legend legend = mLineChart.getLegend();
|
||||
legend.setEnabled(false);
|
||||
|
||||
mLineChart.setData(data);
|
||||
mLineChart.invalidate();
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue