From b4ae4209c50be49f21e923fe24e84926f70b2ddd Mon Sep 17 00:00:00 2001 From: Alexandra Date: Fri, 8 Jun 2018 00:53:05 +0300 Subject: [PATCH 1/2] Working App --- .../.idea/caches/build_file_checksums.ser | Bin 552 -> 552 bytes Ganttproject_mobile/.idea/misc.xml | 2 +- Ganttproject_mobile/.idea/vcs.xml | 6 ++ Ganttproject_mobile/app/build.gradle | 21 +++- .../app/src/main/AndroidManifest.xml | 6 ++ .../ganttproject_mobile/Constants.java | 5 + .../ganttproject_mobile/MainActivity.java | 11 +- .../ganttproject_mobile/TaskAdapter.java | 102 ++++++++++++++++++ .../ganttproject_mobile/TasksActivity.java | 93 ++++++++++++++++ .../app/src/main/proto/dataformat.proto | 10 ++ .../src/main/res/layout/activity_tasks.xml | 41 +++++++ .../app/src/main/res/layout/task_row.xml | 20 ++++ .../app/src/main/res/values/strings.xml | 1 + Ganttproject_mobile/build.gradle | 1 + 14 files changed, 312 insertions(+), 7 deletions(-) create mode 100644 Ganttproject_mobile/.idea/vcs.xml create mode 100644 Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/Constants.java create mode 100644 Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TaskAdapter.java create mode 100644 Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TasksActivity.java create mode 100644 Ganttproject_mobile/app/src/main/proto/dataformat.proto create mode 100644 Ganttproject_mobile/app/src/main/res/layout/activity_tasks.xml create mode 100644 Ganttproject_mobile/app/src/main/res/layout/task_row.xml diff --git a/Ganttproject_mobile/.idea/caches/build_file_checksums.ser b/Ganttproject_mobile/.idea/caches/build_file_checksums.ser index 0c9b48adb34852ac4d40d2f1c6e4ea0644b08836..8ff27e65c98f6c8580d2c51c51e949f6bade8037 100644 GIT binary patch delta 56 zcmV-80LTBR1gHd%m;~H9JVBA1cM#nl9xo - + diff --git a/Ganttproject_mobile/.idea/vcs.xml b/Ganttproject_mobile/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Ganttproject_mobile/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Ganttproject_mobile/app/build.gradle b/Ganttproject_mobile/app/build.gradle index 2fa9529..a4c4e2a 100644 --- a/Ganttproject_mobile/app/build.gradle +++ b/Ganttproject_mobile/app/build.gradle @@ -3,9 +3,10 @@ apply plugin: 'com.google.protobuf' android { compileSdkVersion 27 + buildToolsVersion "27.0.3" defaultConfig { applicationId "com.bardsoftware.ganttproject_mobile" - minSdkVersion 15 + minSdkVersion 23 targetSdkVersion 27 versionCode 1 versionName "1.0" @@ -19,6 +20,9 @@ android { } } +/*sourceSets { + main.java.srcDirs += "${protobuf.generatedFilesBaseDir}/main/javalite" +}*/ protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.0.0' @@ -34,7 +38,7 @@ protobuf { remove java } task.plugins { - javalite { } + javalite {} } } } @@ -44,9 +48,20 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' - testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'com.android.support:recyclerview-v7:27.1.1' + + implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation 'com.google.protobuf:protobuf-lite:3.0.0' + + implementation 'com.jakewharton:butterknife:8.8.1' + annotationProcessor 'com.jakewharton:butterknife-compiler:8.8' + + testImplementation 'junit:junit:4.12' + androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', { + exclude group: 'com.android.support', module: 'support-annotations' + }) } diff --git a/Ganttproject_mobile/app/src/main/AndroidManifest.xml b/Ganttproject_mobile/app/src/main/AndroidManifest.xml index bbcc2fd..c31ee94 100644 --- a/Ganttproject_mobile/app/src/main/AndroidManifest.xml +++ b/Ganttproject_mobile/app/src/main/AndroidManifest.xml @@ -2,6 +2,11 @@ + + + + + + \ No newline at end of file diff --git a/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/Constants.java b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/Constants.java new file mode 100644 index 0000000..aec4a0b --- /dev/null +++ b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/Constants.java @@ -0,0 +1,5 @@ +package com.bardsoftware.ganttproject_mobile; + +public class Constants { + public static final String FILE_PATH = "FilePath"; +} diff --git a/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/MainActivity.java b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/MainActivity.java index c48c5a0..e18a64b 100644 --- a/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/MainActivity.java +++ b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/MainActivity.java @@ -16,14 +16,19 @@ protected void onCreate(Bundle savedInstanceState) { } public void openProtoFile(View view) { - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("*/*"); + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + intent.setType("application/octet-stream"); startActivityForResult(intent, OPEN_FILE_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == OPEN_FILE_CODE && resultCode == RESULT_OK && data != null) { String filePath = data.getDataString(); - // do somthing... + Bundle b = new Bundle(); + b.putString(Constants.FILE_PATH, filePath); + Class TasksActivity = TasksActivity.class; + Intent intent = new Intent(this, TasksActivity); + intent.putExtras(b); + startActivity(intent); } super.onActivityResult(requestCode, resultCode, data); diff --git a/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TaskAdapter.java b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TaskAdapter.java new file mode 100644 index 0000000..2388fe9 --- /dev/null +++ b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TaskAdapter.java @@ -0,0 +1,102 @@ +package com.bardsoftware.ganttproject_mobile; + +import android.content.Context; +import android.graphics.Paint; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.util.SparseBooleanArray; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.TextView; + +import java.util.ArrayList; + +import butterknife.BindView; +import butterknife.ButterKnife; +import static dataformat.Dataformat.Task; +import static dataformat.Dataformat.Tasks; + +public class TaskAdapter extends RecyclerView.Adapter{ + private Tasks tasks; + private Context mContext; + private SparseBooleanArray taskStateArray = new SparseBooleanArray(); + private ArrayList tasksNames=new ArrayList(); + + @NonNull + @Override + public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + Context context = parent.getContext(); + LayoutInflater inflater = LayoutInflater.from(context); + View view = inflater.inflate(R.layout.task_row, parent, false);; + return new TaskAdapter.TaskViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) { + if (tasks == null) return; + String taskName = tasks.getTask(position).getName(); + boolean itemIsChecked = tasks.getTask(position).getIsDone(); + taskStateArray.append(position, itemIsChecked); + tasksNames.add(taskName); + holder.taskView.setText(taskName); + holder.taskCheckbox.setChecked(itemIsChecked); + if (itemIsChecked) { + holder.taskView.setPaintFlags(holder.taskView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + } + holder.itemView.setTag(taskName); + + } + + @Override + public int getItemCount() { + if (tasks == null) return 0; + return tasks.getTaskCount(); + } + + public void setData(Tasks tasks1, Context context) { + tasks = tasks1; + mContext = context; + notifyDataSetChanged(); + } + + public Tasks getTasks() { + Tasks.Builder tasks1 = Tasks.newBuilder(); + for (int i=0; i< getItemCount(); i++){ + Task task = Task.newBuilder() + .setName(tasksNames.get(i)) + .setIsDone(taskStateArray.valueAt(i)) + .build(); + tasks1.addTask(task); + } + return tasks1.build(); + } + + public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ + @BindView(R.id.task_textview) + TextView taskView; + @BindView(R.id.task_checkbox) + CheckBox taskCheckbox; + + public TaskViewHolder(View view) { + super(view); + ButterKnife.bind(this, view); + taskCheckbox.setOnClickListener(this); + } + + @Override + public void onClick(View view) { + + int adapterPosition = getAdapterPosition(); + if (!taskStateArray.get(adapterPosition, false)) { + taskCheckbox.setChecked(true); + taskView.setPaintFlags(taskView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + taskStateArray.put(adapterPosition, true); + } else { + taskCheckbox.setChecked(false); + taskStateArray.put(adapterPosition, false); + } + } + } +} diff --git a/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TasksActivity.java b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TasksActivity.java new file mode 100644 index 0000000..60e6c7f --- /dev/null +++ b/Ganttproject_mobile/app/src/main/java/com/bardsoftware/ganttproject_mobile/TasksActivity.java @@ -0,0 +1,93 @@ +package com.bardsoftware.ganttproject_mobile; + +import android.Manifest; +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import butterknife.BindView; + +import static dataformat.Dataformat.Tasks; + +public class TasksActivity extends AppCompatActivity { + Uri filePath; + Context mContext; + private TaskAdapter taskAdapter; + Tasks tasks; + @BindView(R.id.tasks_recyclerview) + RecyclerView tasksRecyclerView; + private static final int REQUEST_EXTERNAL_STORAGE = 1; + private static String[] PERMISSIONS_STORAGE = { + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_tasks); + mContext=this; + if (savedInstanceState == null) { + Bundle bundle = getIntent().getExtras(); + filePath = Uri.parse(bundle.getString(Constants.FILE_PATH)); + } + verifyStoragePermissions(this); + + try { + InputStream input = mContext.getContentResolver().openInputStream(filePath); + tasks = Tasks.parseFrom(input); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + //tasksRecyclerView = findViewById(R.id.tasks_recyclerview); + tasksRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + taskAdapter = new TaskAdapter(); + tasksRecyclerView.setAdapter(taskAdapter); + taskAdapter.setData(tasks, this); + } + + public static void verifyStoragePermissions(Activity activity) { + // Check if we have write permission + int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); + + if (permission != PackageManager.PERMISSION_GRANTED) { + // We don't have permission so prompt the user + ActivityCompat.requestPermissions( + activity, + PERMISSIONS_STORAGE, + REQUEST_EXTERNAL_STORAGE + ); + } + } + + public void saveTasksFile(View view) { + tasks=taskAdapter.getTasks(); + mContext.grantUriPermission("com.bardsoftware.ganttproject_mobile", filePath, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + try { + + OutputStream output = mContext.getContentResolver().openOutputStream(filePath); + tasks.writeTo(output); + output.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/Ganttproject_mobile/app/src/main/proto/dataformat.proto b/Ganttproject_mobile/app/src/main/proto/dataformat.proto new file mode 100644 index 0000000..4e2f2bd --- /dev/null +++ b/Ganttproject_mobile/app/src/main/proto/dataformat.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; +package dataformat; + +message Task { + string name = 1; + bool is_done = 2; +} +message Tasks { + repeated Task task= 1; +} \ No newline at end of file diff --git a/Ganttproject_mobile/app/src/main/res/layout/activity_tasks.xml b/Ganttproject_mobile/app/src/main/res/layout/activity_tasks.xml new file mode 100644 index 0000000..b01c31b --- /dev/null +++ b/Ganttproject_mobile/app/src/main/res/layout/activity_tasks.xml @@ -0,0 +1,41 @@ + + + + + + + + + +