Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/captures
.externalNativeBuild
/.idea
/keystores/release.properties
/keystores/release_keystore.jks
36 changes: 31 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
apply plugin: 'com.android.application'


def singingProperties = new Properties()

android {
compileSdkVersion 26
defaultConfig {
Expand All @@ -10,15 +13,38 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
signingConfigs {
debug {
singingProperties.load(new FileInputStream(file("..\\keystores\\debug.properties")))
keyAlias singingProperties.debugSignKeyAlias
keyPassword singingProperties.debugSignKeyPassword
storeFile file(singingProperties.debugStoreFilePath)
storePassword singingProperties.debugStorePassword

}
release {
singingProperties.load(new FileInputStream(file("..\\keystores\\release.properties")))
keyAlias singingProperties.releaseSignKeyAlias
keyPassword singingProperties.releaseSignKeyPassword
storeFile file(singingProperties.releaseStoreFilePath)
storePassword singingProperties.releaseStorePassword

}
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.debug
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Expand Down
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package="com.example.anna.myapplication">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name=".presentation.MyApplication"
Expand All @@ -26,7 +28,14 @@
<activity android:name=".presentation.CatAsyncTaskActivity" />
<activity android:name=".presentation.FrescoImageActivity" />
<activity android:name=".presentation.SQLiteDBActivity" />
<activity android:name=".presentation.RoomDB"></activity>
<activity android:name=".presentation.RoomDB" />

<service
android:name=".data.LoadDatabaseIntentService"
android:exported="false" />

<activity android:name=".presentation.IntentServiceActivity" />
<activity android:name=".presentation.TouchActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.anna.myapplication.data;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.anna.myapplication.presentation.MyApplication;

public class LoadDatabaseIntentService extends IntentService {

private FileOutputStream fileOutputStream = null;

public LoadDatabaseIntentService() {
super("LoadDatabaseIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
String databaseBackup = MyApplication.getPersonDao().loadAll().toString();
String fileName = intent.getStringExtra("fileName");
try {
fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
fileOutputStream.write(databaseBackup.getBytes());
fileOutputStream.close();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так неправильно закрывать stream. Используй try with resources лучше

} catch (Exception e) {
Toast toast = Toast.makeText(this, "An error occurred while writing database", Toast.LENGTH_LONG);
toast.show();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

public interface PersonContract extends BaseColumns {


String TABLE_NAME = "personsTable";

String NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,29 @@ public void setBirthday(String birthday) {

@Override
public String toString() {
String returnedString = "Person{id=";
if (id == NOT_SPECIFIED_LONG) {
return "Person{" + "id=?, name='" + name + "\'" + '}';
returnedString = returnedString.concat("?");
} else {
return "Person{" + "id=" + id + ", name='" + name + "\'" + '}';
returnedString = returnedString.concat("" + id);
}
if (!name.equals(NOT_SPECIFIED_STRING)) {
returnedString = returnedString.concat(", name='" + name + "\'");
}
if (!note.equals(NOT_SPECIFIED_STRING)) {
returnedString = returnedString.concat(", note='" + note + "\'");
}
if (imageRes != NOT_SPECIFIED_INT) {
returnedString = returnedString.concat(", imageRes=" + imageRes);
}
if (!imageLink.equals(NOT_SPECIFIED_STRING)) {
returnedString = returnedString.concat(", imageLink='" + imageLink + "\'");
}
if (!birthday.equals(NOT_SPECIFIED_STRING)) {
returnedString = returnedString.concat(", birthday=" + birthday);
}
returnedString = returnedString.concat("}");
return returnedString;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.anna.myapplication.presentation;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.example.anna.myapplication.R;

import java.io.FileInputStream;

public class IntentServiceActivity extends AppCompatActivity {

private TextView DatabaseTextView;
private Button DatabaseLoadButton, nextButton, backButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_service);

DatabaseTextView = (TextView) findViewById(R.id.textView);

DatabaseLoadButton = (Button) findViewById(R.id.loadDatabaseButton);
DatabaseLoadButton.setOnClickListener(view -> {
StringBuilder stringBuilder = new StringBuilder();
try{
FileInputStream fstream = openFileInput(MyApplication.getFileName());
int i;
while ((i = fstream.read())!= -1){
stringBuilder.append((char)i);
}
fstream.close();
DatabaseTextView.setText(stringBuilder.toString());
}catch (Exception e){
DatabaseTextView.setText(getResources().getString(R.string.error_load_database));
}

});

nextButton = findViewById(R.id.nextScreenButton);
nextButton.setOnClickListener(view -> {
Intent startActivity = new Intent(this, TouchActivity.class);
startActivity(startActivity);
});

backButton = findViewById(R.id.to_first_activity_button);
backButton.setOnClickListener(view -> {
Intent startActivity = new Intent(this, MainActivity.class);
startActivity(startActivity);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {

buttonTraining = findViewById(R.id.training_thread);
buttonTraining.setOnClickListener(view -> {
Intent startActivity = new Intent(MainActivity.this, RoomDB.class);
Intent startActivity = new Intent(MainActivity.this, TouchActivity.class);
startActivity(startActivity);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,39 @@

import android.app.Application;
import android.arch.persistence.room.Room;
import android.content.Intent;

import com.example.anna.myapplication.data.LoadDatabaseIntentService;
import com.example.anna.myapplication.data.AppDatabase;
import com.example.anna.myapplication.data.DatabaseHolder;
import com.example.anna.myapplication.data.PersonDao;
import com.example.anna.myapplication.data.PersonRepository;
import com.facebook.drawee.backends.pipeline.Fresco;

import java.util.Date;

public class MyApplication extends Application {

private static AppDatabase database;
private static PersonRepository personRepository;
private static DatabaseHolder databaseHolder;
private static String fileName;

public static PersonDao getPersonDao(){
return database.PersonDao();
}
public static PersonRepository getRepository(){
return personRepository;
}
public static String getFileName(){
return fileName;
}

private String getFileNameOnCurrentTime(){
Date currentDate = new Date();
long millisFromStart = currentDate.getTime();
return "backup-" + millisFromStart;
}

@Override
public void onCreate() {
Expand All @@ -36,6 +50,11 @@ public void onCreate() {
.build();

setInitialData();

// Запускаем IntentService
Intent intentMyIntentService = new Intent(this, LoadDatabaseIntentService.class);
fileName = getFileNameOnCurrentTime();
startService(intentMyIntentService.putExtra("fileName", fileName));
}

public static void setInitialData(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.anna.myapplication.R;
Expand All @@ -18,6 +18,7 @@ public class PersonListActivity extends AppCompatActivity {
private Fragment fragment;
private Fragment newFragment;
private long personId = -1;
private float downX, downY;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -132,4 +133,45 @@ public void openFragment2 (long personId_) {

findViewById(R.id.fragmentContainer2).setVisibility(View.VISIBLE);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вместо рукописной обработки используй CompatGestureDetector

super.onTouchEvent(event);

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
float upX = event.getX();
float upY = event.getY();

float deltaX = downX - upX;
float deltaY = downY - upY;
if ((Math.abs(deltaY) > Math.abs(deltaX)) && (deltaY < 0)) {

personId = -1;

if (findViewById(R.id.fragmentContainer2).getVisibility() == View.VISIBLE) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавь детектор только на экране(фрагменте) профиля человека, тогда достаточно вызвать onBackPressed у активити вместо этого кода

findViewById(R.id.fragmentContainer2).setVisibility(View.GONE);

fragment = fragmentManager.findFragmentById(R.id.fragmentContainer1);
newFragment = new PersonListFragment();

if (fragment == null) {
fragmentManager.beginTransaction()
.addToBackStack(null)
.add(R.id.fragmentContainer1, newFragment)
.commit();
}

findViewById(R.id.fragmentContainer1).setVisibility(View.VISIBLE);
}
}
break;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ protected void onCreate(Bundle savedInstanceState) {

nextButton = findViewById(R.id.nextScreenButton);
nextButton.setOnClickListener(view -> {
// Intent startActivity = new Intent(this, RoomDB.class);
//startActivity(startActivity);
Intent startActivity = new Intent(this, IntentServiceActivity.class);
startActivity(startActivity);
});

backButton = findViewById(R.id.to_first_activity_button);
Expand Down
Loading