@@ -163,11 +163,15 @@ public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_profile_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new ProfileFragment()).commit();
LevelsFragment levelsFragment = new LevelsFragment();
levelsFragment.setLesson(true);
fragmentManager.beginTransaction().replace(R.id.content_frame, levelsFragment).commit();
} else if (id == R.id.nav_score_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new ScoreFragment()).commit();
} else if (id == R.id.nav_stories_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new LevelsFragment()).commit();
LevelsFragment levelsFragment = new LevelsFragment();
levelsFragment.setLesson(false);
fragmentManager.beginTransaction().replace(R.id.content_frame, levelsFragment).commit();
} else if (id == R.id.nav_top_scores_layout) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new TopScoresFragment()).commit();
}
@@ -1,23 +1,28 @@
package com.read.storybook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.read.storybook.model.Question;
import com.read.storybook.model.Story;
import com.read.storybook.util.AppConstants;

import java.util.List;
import java.util.Random;


public class PageStoryFragment extends Fragment {
@@ -27,20 +32,24 @@ public class PageStoryFragment extends Fragment {
public static final String IS_LAST = "IS_LAST";
public static final String STORY = "STORY";
public static final String PAGING = "PAGING";
public static final String PAGE_TO_SHOW = "PAGE_TO_SHOW";
public static final String PAGE_LEFT = "PAGE_LEFT";
public static final String IS_LESSON = "IS_LESSON";

public PageStoryFragment() { }

public static final PageStoryFragment newInstance(String message, Bitmap bitmap, boolean isLast, Story story, String paging)
public static final PageStoryFragment newInstance(String message, Bitmap bitmap, boolean isLast, Story story, String paging, boolean hasLesson, boolean isLesson, String pageLeft)
{
PageStoryFragment f = new PageStoryFragment();
Bundle bdl = new Bundle(1);
bdl.putString(PAGE_TITLE, message);
bdl.putParcelable(IMAGE, bitmap);
bdl.putBoolean(IS_LAST,isLast);
bdl.putString(PAGING, paging);
if(isLast) {
bdl.putSerializable(STORY, story);
}
bdl.putString(PAGE_LEFT, pageLeft);
bdl.putBoolean(PAGE_TO_SHOW,hasLesson);
bdl.putBoolean(IS_LESSON,isLesson);
bdl.putSerializable(STORY, story);
f.setArguments(bdl);
return f;
}
@@ -51,11 +60,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
String message = getArguments().getString(PAGE_TITLE);
Bitmap bitmap = getArguments().getParcelable(IMAGE);
boolean isLast = getArguments().getBoolean(IS_LAST);
final Story story = isLast ? (Story)getArguments().getSerializable(STORY) : null;
final Story story = (Story)getArguments().getSerializable(STORY);
String pageLeft = getArguments().getString(PAGE_LEFT);
View v = inflater.inflate(R.layout.fragment_page_story, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.storyTitle);
messageTextView.setText("");
ImageView iv = (ImageView) v.findViewById(R.id.imageStory);
RelativeLayout parent = (RelativeLayout)v.findViewById(R.id.myfragment_layout);
iv.setImageBitmap(bitmap);
iv.setMinimumWidth(3000);
iv.setMinimumHeight(1000);
@@ -74,8 +85,94 @@ public void onClick(View arg0) {
}
});
TextView pageNo = v.findViewById(R.id.paging);

pageNo.setText(getArguments().getString(PAGING));
if(getArguments().getBoolean(PAGE_TO_SHOW)){
createWindow(40,parent, "Lesson Time!",story,pageNo.getText().toString(), false,null);
parent.invalidate();
}
if(getArguments().getBoolean(IS_LESSON) && isLast){
createWindow(30,parent, "You have finished the ",story,pageNo.getText().toString(), true,pageLeft);
parent.invalidate();
}
return v;
}

private void createWindow(int textSize, final RelativeLayout parent, String str, final Story s, final String page, final boolean isLesson, final String pageLeft){
final RelativeLayout window = createRelativeLayout(parent,isLesson);
TextView message = createTextView(str,textSize,20,0,window,RelativeLayout.LayoutParams.MATCH_PARENT);
message.setTextColor(Color.argb(200, 255, 255, 255));
message.setAllCaps(false);
String txtButton = "OK";
TextView button = null;
if(isLesson){
TextView subMessage = createTextView("lesson!",textSize,100,0,window,RelativeLayout.LayoutParams.MATCH_PARENT);
subMessage.setTextColor(Color.argb(200, 255, 255, 255));
subMessage.setAllCaps(false);
txtButton = " Return to story ";
button = createTextView(txtButton,20,215,220,window,RelativeLayout.LayoutParams.WRAP_CONTENT);
}else{
button = createTextView(txtButton,30,170,320,window,RelativeLayout.LayoutParams.WRAP_CONTENT);
button.getLayoutParams().width = 150;
}

button.setTextColor(Color.argb(180, 255, 255, 255));
button.setBackgroundColor(Color.argb(200,108,186,249));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isLesson){
Intent myIntent = new Intent(parent.getContext(), LessonActivity.class);
s.setLessons(null);
myIntent.putExtra(AppConstants.STORY_OBJ,s);
myIntent.putExtra(LessonActivity.CURRENT_PAGE,page.split(" of ")[0]);
parent.getContext().startActivity(myIntent);
getActivity().finish();
}else{
Intent myIntent = new Intent(parent.getContext(), StoryActivity.class);
s.setLessons(null);
myIntent.putExtra(AppConstants.STORY_OBJ,s);
myIntent.putExtra(LevelsActivity.IS_LESSON,"false");
myIntent.putExtra(LessonActivity.CURRENT_PAGE,pageLeft);
parent.getContext().startActivity(myIntent);
getActivity().finish();
}

}
});
}

private RelativeLayout createRelativeLayout(RelativeLayout parent, boolean isLesson){
RelativeLayout rl = new RelativeLayout(parent.getContext());
rl.setBackgroundColor(Color.argb(130, 0, 0, 0));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if(isLesson){
lp.height = 320;
}else{
lp.height = 300;
}

lp.width = 800;
lp.setMargins(500,300,0,0);

rl.setLayoutParams(lp);
parent.addView(rl);
return rl;
}
public static TextView createTextView(String str, int textSize, int top, int left, RelativeLayout layout,int w){
TextView tv = new TextView(layout.getContext());
tv.setTextSize(textSize);
tv.setText(str);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
w,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.topMargin = top;
lp.leftMargin = left;
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setLayoutParams(lp);
layout.addView(tv);
return tv;
}
}
@@ -31,11 +31,13 @@
public class StoriesActivity extends AppCompatActivity {
ListView lv;
Level level;
private boolean isLesson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stories);
level = (Level)getIntent().getSerializableExtra(AppConstants.LEVEL_NAME);
isLesson = Boolean.valueOf(getIntent().getStringExtra(LevelsActivity.IS_LESSON));
lv= (ListView)findViewById(R.id.list);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@@ -110,6 +112,6 @@ private List<Story> createList(JSONObject resp){
}
private void addStories(List<Story> stories){

lv.setAdapter(new CustomStoryAdapter(this, stories));
lv.setAdapter(new CustomStoryAdapter(this, stories, isLesson));
}
}
@@ -21,8 +21,10 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.read.storybook.Image.ImageLoader;
import com.read.storybook.model.Choice;
import com.read.storybook.model.Image;
import com.read.storybook.model.Lesson;
import com.read.storybook.model.Question;
import com.read.storybook.model.Story;
import com.read.storybook.model.User;
@@ -44,107 +46,91 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StoryActivity extends FragmentActivity{
Story story;
TextView title,status;
TextView title;
MyPageAdapter pageAdapter;
Story tempStory = new Story();
Button createExam,btnNarrative,btnLesson;
Button playAudio;
Player mediaPlayer;
ImageLoader imageLoader;
boolean isLesson;
Integer pageToShow;
String pageToLoad;
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
int ctr = 0;

if(!isLesson && pageToLoad == null) {
if (pageToShow == null && tempStory.getLessons() != null && tempStory.getLessons().size() > 0) {
pageToShow = generateRandomNumbers(tempStory.getLessons().size());
}
}
tempStory.setSound(story.getSound());
for(Image i : story.getImages()){
String page = (ctr + 1) + " of " + story.getImages().size();
fList.add(PageStoryFragment.newInstance(title.getText().toString(), i.getBitmap(), (ctr + 1) == story.getImages().size(), tempStory, page));
boolean hasLesson = pageToShow != null ? pageToShow == (ctr + 1): false;
fList.add(PageStoryFragment.newInstance(title.getText().toString(), i.getBitmap(), (ctr + 1) == story.getImages().size(), tempStory, page, hasLesson, false,null));
ctr++;
}
return fList;
}
private int generateRandomNumbers(int max){
Random rand = new Random();
Integer n = 0;
int median = (int)Math.ceil((1 + max) / 2.0);
n = rand.nextInt(max + 1 - median) + median;
if( n > max){
n = n - max;
}
return n;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story);
status = (TextView) findViewById(R.id.status);
status.setTextSize(10);
createExam = (Button) findViewById(R.id.createExam);
playAudio = (Button) findViewById(R.id.playAudio);
playAudio.setEnabled(false);
mediaPlayer = new Player(status,playAudio);
playAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying()){
playAudio.setBackground(getDrawable(R.drawable.mic_unmute));
mediaPlayer.stop();
}else{
playAudio.setBackground(getDrawable(R.drawable.mic_mute));
mediaPlayer.play();
}
playAudio.invalidate();
}
});
btnNarrative = (Button) findViewById(R.id.btnNarrative);
btnNarrative.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(StoryActivity.this, AddLessonNarrativeActivity.class);
myIntent.putExtra(AppConstants.STORY_ID,story.getId());
myIntent.putExtra(AppConstants.STORY_NARRATIVE,story.getId());
StoryActivity.this.startActivity(myIntent);
finish();
}
});
btnLesson = (Button) findViewById(R.id.btnLesson);
btnLesson.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(StoryActivity.this, AddLessonNarrativeActivity.class);
myIntent.putExtra(AppConstants.STORY_ID,story.getId());
myIntent.putExtra(AppConstants.STORY_LESSON,story.getId());
StoryActivity.this.startActivity(myIntent);
mediaPlayer.stop();
finish();
}
});
//remove this
btnLesson.setVisibility(View.VISIBLE);
createExam.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(StoryActivity.this, AddQuestionActivity.class);
myIntent.putExtra(AppConstants.STORY_ID,story.getId());
StoryActivity.this.startActivity(myIntent);
mediaPlayer.stop();
finish();
}
});

title = (TextView) findViewById(R.id.mainStoryTitle);
isLesson = Boolean.valueOf(getIntent().getStringExtra(LevelsActivity.IS_LESSON));
story = (Story)getIntent().getSerializableExtra(AppConstants.STORY_OBJ);
pageToLoad = getIntent().getStringExtra(LessonActivity.CURRENT_PAGE);
if(!isLesson){
tempStory.setTitle(story.getTitle());
imageLoader = new ImageLoader(this, story, tempStory, pageToLoad == null);
}
title = (TextView) findViewById(R.id.mainStoryTitle);
title.setText(story.getTitle());
searchImages(story);

}

private void searchImages(final Story story){
Service service = new Service("Loading resources...", StoryActivity.this, new ServiceResponse() {
@Override
public void postExecute(JSONObject resp) {
try {
getImages(resp, story);
if(!isLesson && pageToLoad == null){
searchLesson(story);
}
getImages(resp, story, true);
}catch (Exception e){e.printStackTrace();}
}
});
StoryService.searchImages(story.getId(), service);
if(isLesson){
StoryService.searchLessons(story.getId(), service);
}else{
StoryService.searchImages(story.getId(), service);
}

}
private void getBitmaps(final Story story){
private void searchLesson(final Story story){
Service service = new Service("Loading resources...", StoryActivity.this, new ServiceResponse() {
@Override
public void postExecute(JSONObject resp) {
try {
getImages(resp, story, false);
}catch (Exception e){e.printStackTrace();}
}
});
StoryService.searchLessons(story.getId(), service);
}
public void getBitmaps(final Story story){
Service service = new Service("Initializing resources...", StoryActivity.this, new ServiceResponse() {
@Override
public void postExecute(JSONObject resp) {
@@ -153,81 +139,59 @@ public void postExecute(JSONObject resp) {
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
playAudio(story);
if(pageToLoad != null){
pager.setCurrentItem(Integer.valueOf(pageToLoad) - 1);
}
if(imageLoader != null){
imageLoader.playAudio(story);
}

}catch (Exception e){e.printStackTrace();}
}
});
StoryService.populateImages(story, service);
}

private void getImages(JSONObject resp, Story story){
private void getImages(JSONObject resp, Story story,boolean executeBitmap){
JSONArray arr = resp.optJSONArray("records");
for( int i=0; i< arr.length(); i++){
JSONObject obj = arr.optJSONObject(i);
story.addImage(new Image(obj.optString("image")));
}
getQuestions(story.getId(), story);
}

private List<Question> createQuestions(JSONObject resp){
List<Question> questions = new ArrayList<Question>();
try {
JSONArray arr = resp.optJSONArray("records");
if(arr == null){
return null;
if(isLesson){
if(arr == null) {
title.setText("No available lesson.");
}else{
title.setText("Lesson for " + title.getText().toString());
}
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.optJSONObject(i);
Question q = new Question();
q.setId(obj.optString("QUESTION_ID"));
q.setDescription(obj.optString("DESCRIPTION"));
q.setActive(obj.optString("ISACTIVE").equals("1"));
JSONArray arr2 = new JSONObject(obj.optString("choices")).optJSONArray("choices");
for (int j = 0; j < arr2.length(); j++) {
JSONObject obj2 = arr2.optJSONObject(j);
Choice c = new Choice();
c.setId(obj2.optString("CHOICE_ID"));
c.setDescription(obj2.optString("DESCRIPTION"));
c.setActive(obj2.optString("isActive").equals("1"));
c.setAnswer(obj2.optString("isAnswer").equals("1"));
q.addChoice(c);
}else{
if(!executeBitmap) {
if (arr == null) {
imageLoader.setBtnLessonVisible(View.VISIBLE);
} else {
imageLoader.setBtnLessonVisible(View.INVISIBLE);
}
questions.add(q);
}
}catch (Exception e){ e.printStackTrace();}
return questions;
}
private void getQuestions(final String storyId, final Story story){
Service service = new Service("Loading resources...", StoryActivity.this, new ServiceResponse() {
@Override
public void postExecute(JSONObject resp) {
try {
tempStory.setId(storyId);
tempStory.setQuestions( createQuestions(resp));
User user = AppCache.getInstance().getUser();
if(user.isAdmin() && tempStory != null && tempStory.getQuestions() == null){
createExam.setVisibility(View.VISIBLE);
}
if(user.isAdmin() && story.getSound() == null){
btnNarrative.setVisibility(View.VISIBLE);
}
getBitmaps(story);
}catch (Exception e){e.printStackTrace();}
}
for( int i=0; i< arr.length(); i++){
JSONObject obj = arr.optJSONObject(i);
if(!executeBitmap){
tempStory.addLesson(new Lesson(obj.optString("image")));
}else{
story.addImage(new Image(obj.optString("image")));
}
});
QuestionService.getQuestions(storyId, service);

}
if(executeBitmap){
if(imageLoader != null){
imageLoader.getQuestions(story.getId(), story);
}
getBitmaps(story);
}
}


@Override
public void onBackPressed() {
super.onBackPressed();
mediaPlayer.stop();
}
private void playAudio(final Story story){
if(story.getSound() != null){
mediaPlayer.execute(story.getSound().getUrl());
playAudio.setVisibility(View.VISIBLE);
status.setVisibility(View.VISIBLE);
if(imageLoader != null){
imageLoader.getMediaPlayer().stop();
}
}

}
@@ -21,6 +21,9 @@ public class Story extends BaseModel{
private String title;
private String cover;
private List<Lesson> lessons;

public void setLessons(List<Lesson> lessons) { this.lessons = lessons; }

//-- Transient
private List<Question> questions;
private List<Image> images;
@@ -82,7 +82,11 @@ public static void searchImages(String storyId, Service service){
service.get("http://jabahan.com/storybook/story/searchimages.php?id="+storyId, params);
service.execute();
}

public static void searchLessons(String storyId, Service service){
RequestParams params = new RequestParams();
service.get("http://jabahan.com/storybook/story/searchLessons.php?id="+storyId, params);
service.execute();
}
public static void delete(Context activity, String storyId, Service service){
RequestParams params = new RequestParams();
params.put("storyId", storyId);
@@ -16,9 +16,11 @@ public class Player extends AsyncTask<String, Void, Boolean> {
private MediaPlayer mediaPlayer;
private boolean initialStage = true;
private TextView status;
public Player(TextView status, Button btn){
boolean isPlayingDefault;
public Player(TextView status, Button btn, boolean isPlayingDefault){
this.status = status;
this.btn = btn;
this.isPlayingDefault = isPlayingDefault;
}
@Override
protected Boolean doInBackground(String... strings) {
@@ -57,11 +59,18 @@ protected void onPostExecute(Boolean aBoolean) {
status.setText("Error loading sound...");
return;
}
status.setText("Narrative Playing...");
if(isPlayingDefault){
status.setText("Narrative Playing...");
mediaPlayer.start();
isPlaying = true;
}else{
status.setText("Narrative Paused...");
mediaPlayer.pause();
isPlaying = false;
}
btn.setEnabled(true);
mediaPlayer.start();
initialStage = false;
isPlaying = true;

}

@Override
@@ -84,8 +93,11 @@ public void play(){
mediaPlayer.start();
}
public void stop(){
isPlaying = false;
status.setText("Narrative Paused.");
mediaPlayer.pause();
if(mediaPlayer != null){
isPlaying = false;
status.setText("Narrative Paused.");
mediaPlayer.pause();
}

}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.read.storybook.LessonActivity">
<TextView
android:id="@+id/mainStoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textPersonName"
android:text="text"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >

</android.support.v4.view.ViewPager>
</RelativeLayout>
@@ -5,7 +5,7 @@
<item
android:id="@+id/nav_profile_layout"
android:icon="@drawable/icon_profile"
android:title="Profile" />
android:title="Lessons" />
<item
android:id="@+id/nav_score_layout"
android:icon="@drawable/icon_score"