Skip to content

Commit

Permalink
Added DoodleActivity, it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman-al-masoud committed Aug 11, 2021
1 parent baece5b commit 2f817f4
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 15 deletions.
17 changes: 3 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application



android:allowBackup="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Noadpadlight">
<activity android:name=".ui.SettingsActivity"></activity>
<activity android:name=".ui.DoodleActivity"></activity>
<activity android:name=".ui.SettingsActivity" />
<activity android:name=".ui.PagesActivity" />
<activity android:name=".ui.ReaderActivity" />
<activity
Expand All @@ -29,24 +27,15 @@
</intent-filter>
</activity>



<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.luxlunaris.fileprovider"
android:exported="false"
android:grantUriPermissions="true">

<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />

</provider>


</application>




</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.luxlunaris.noadpadlight.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.luxlunaris.noadpadlight.R;

import java.io.File;

public class DoodleActivity extends AppCompatActivity {


DoodleView doodleView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_doodle);

ConstraintLayout l = findViewById(R.id.doodle_activity_layout);

doodleView = new DoodleView(this);

l.addView(doodleView);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

//inflate the menu's layout xml
getMenuInflater().inflate(R.menu.doodle_activity_toolbar, menu);

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()){
case R.id.done_doodling:
File doodleFile = doodleView.getSnapshot();
Intent resultIntent = new Intent();
resultIntent.putExtra("DOODLE_FILE", doodleFile);
setResult(RESULT_OK, resultIntent);
finish();
break;

}

return super.onOptionsItemSelected(item);
}
}
112 changes: 112 additions & 0 deletions app/src/main/java/com/luxlunaris/noadpadlight/ui/DoodleView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.luxlunaris.noadpadlight.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DoodleView extends View {


Paint paint;

Path path;


public DoodleView(Context context) {
super(context);
paint = new Paint();
setColor(Color.BLACK);
setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
path =new Path();
}



public void setColor(int color){
paint.setColor(color);
}

public void setStrokeWidth(int width){
paint.setStrokeWidth(width);
}


@Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(Color.WHITE);
canvas.drawPath(path, paint);
}


@Override
public boolean onTouchEvent(MotionEvent event) {

float x = event.getX();
float y = event.getY();
Log.d("TOUCH_EVENT", "x: "+x+" y: "+y);


switch(event.getAction()){

case MotionEvent.ACTION_MOVE:
path.lineTo(x, y);
break;
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
break;

}

//trigger onDraw
invalidate();

//very important
return true;
}


public File getSnapshot(){

File doodleFile = new File(getContext().getFilesDir(), "doodle.png");

try {
FileOutputStream fos = new FileOutputStream(doodleFile);
setDrawingCacheEnabled(true);///
Bitmap b = Bitmap.createBitmap( getDrawingCache());
setDrawingCacheEnabled(false);///

b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

return doodleFile;
}













}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.luxlunaris.noadpadlight.ui;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
Expand All @@ -16,6 +21,7 @@
import com.luxlunaris.noadpadlight.control.classes.SETTINGS_TAGS;
import com.luxlunaris.noadpadlight.control.classes.Settings;
import com.luxlunaris.noadpadlight.model.interfaces.Page;
import com.luxlunaris.noadpadlight.model.services.FileIO;

import java.io.File;

Expand Down Expand Up @@ -253,6 +259,13 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
String currentMode = HTML_EDIT_MODE? getString(R.string.editing_html_mode_ON) : getString(R.string.editing_html_mode_OFF);
Toast.makeText(this, currentMode, Toast.LENGTH_LONG).show();
break;
case R.id.make_doodle:

Intent intent = new Intent(this, DoodleActivity.class);
startActivityForResult(intent, 1);

break;

case R.id.make_bold:
applyTag("b");
break;
Expand Down Expand Up @@ -320,6 +333,34 @@ public void onFileObtained(File file) {
reloadText();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

//halt if result code is not "ok"
if(resultCode != Activity.RESULT_OK){
return;
}

//halt if "data" is null
if(data==null){
return;
}

switch (requestCode){

case 1:
Uri uri = data.getData();
File doodleFile = (File)data.getSerializableExtra("DOODLE_FILE");
saveToPage();
page.addImage(doodleFile.getPath(), textView.getSelectionStart());
reloadText();
break;

}

}

/**
* Save progress if page isn't empty.
* Delete page if it's empty.
Expand All @@ -342,5 +383,4 @@ public void onBackPressed() {
finish();
}


}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_doodle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/doodle_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.DoodleActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/menu/doodle_activity_toolbar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/done_doodling"
android:icon="@android:drawable/checkbox_on_background"
android:title="done"
app:showAsAction="always" />


</menu>
8 changes: 8 additions & 0 deletions app/src/main/res/menu/reader_activity_toolbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
app:showAsAction="always" />


<item
android:id="@+id/make_doodle"
android:icon="@android:drawable/ic_menu_edit"
android:title="add doodle"
/>



<item
android:id="@+id/styles_menu"
android:title="@string/style_menu_title">
Expand Down

0 comments on commit 2f817f4

Please sign in to comment.