text:
package com.frrahat.quransimple;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.text.Spannable;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

/**
 * @author Rahat
 * started: February 21, 2015 at 7:04:18 PM
 */
public class MainActivity extends Activity {

	public static String storageFolderName="QuranForMyAndroid";
	private final String AudioStorageDirName="Audio";
	private File audioStorageDir;

	private InputMethodManager imm;
	private SharedPreferences sharedPrefs;

	private EditText commandText;
	private static EditText mainText;
	private ScrollView scrollView;

	private Button prevButton;
	private Button nextButton;

	private InputCommand CUR_INPUT_COMMAND;

	private int MAX_AYAHS_IN_ONE_PAGE;

	private int PagesToSkipOnLongClick;

	private enum InputMode {
		MODE_VERSE, MODE_SEARCH, MODE_RANDOM
	};

	// for loadinfg texts
	public static final int Max_Quran_Texts = 10;
	//quranText[0] will be always empty
	private static QuranText[] allQuranTexts;

	private static QuranText primaryText;
	private static QuranText secondaryText;
	/*
	 * allQuranText is initialized by this code: if (index == 0) isArabic =
	 * true; Log.i("init", "loading file, index: " + index);
	 * allQuranTexts[index] = new QuranText(getBaseContext(),
	 * resourceIDs[index], isArabic); index++, upto 3
	 */

	//resourceIDs[0] never used, like QuranText[0]
	private int[] resourceIDs = { -1,R.raw.quran_uthmani, R.raw.en_hilali_khan};

	private final static int Word_Info_Index = 0;
	private final static int Arabic_Text_Index = 1;
	private final static int English_Text_Index = 2;
	public static final int Total_Default_Quran_Texts = 3;

	private final int Search_Operand_Only_English=0;
	private final int Search_Operand_Pri_And_Secondary=1;
	private final int Search_Operand_All=2;

	private static int PRIMARY_TEXT_INDEX;
	private int SECONDARY_TEXT_INDEX = Word_Info_Index;
	private int Search_Operand_Text_Id=Search_Operand_Pri_And_Secondary;
	private int MAX_SEARCH_COUNT;

	// private enum Text{Arabic, EngLish, Bengali};

	// for font faces
	private static ArrayList<Typeface> defaultTypefaces;
	private static String defaultTypefaceNames[]=
		{"jaghb_uni_bold.ttf",
		"amiri-quran.ttf",
		"amiri-regular.ttf",
		"droid_naskh_regular.ttf",
		"me_quran.ttf",
		"siyamrupali.ttf"};

	private boolean isInSearchMode;
	private boolean isRecitaionAudioOn;

	private MediaPlayer mplayer;

	private final int REQUEST_SETTINGS = 0;
	private final int REQUEST_SURAH_LIST = 1;
	private final int REQUEST_BookmarksDisplay=2;

	//According to the array_prefActionOnStart
	private final int ActionOnStartRandomAyah=0;
	private final int ActionOnStartSurahList=1;
	private final int ActionOnStartNone=2;

	private boolean showARandomAyah;

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

		//storageFolderName="."+getApplicationContext().getPackageName();
		initializeComponents();

		//the volume key button is to concentrate on media volume up down when not overriden
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}

	private void initializeComponents() {

		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

		ActionBar actionBar=getActionBar();
		actionBar.setCustomView(R.layout.actionbar_with_edittext);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
                | ActionBar.DISPLAY_SHOW_HOME);

		commandText = (EditText) actionBar.getCustomView().findViewById(R.id.editText_commandText);

		mainText = (EditText) findViewById(R.id.editText_mainText);
		scrollView = (ScrollView) findViewById(R.id.scrollView);

		mainText.setText(" ");// preparing the scrollView(may be)

		commandText.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				hideSoftKeyBoard();

				CUR_INPUT_COMMAND = processInput(commandText.getText()
						.toString());

				if (CUR_INPUT_COMMAND != null) {
					printAllAyahs();
				}

				return true;
			}
		});

		mainText.setFocusable(false);
		//mainText.setLongClickable(false);

		prevButton = (Button) findViewById(R.id.button_prev);
		nextButton = (Button) findViewById(R.id.button_next);

		prevButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				actionOnPrevButtonClick();
			}
		});
		prevButton.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				if (CUR_INPUT_COMMAND != null) {
					skipPages(PagesToSkipOnLongClick, -1);
					CUR_INPUT_COMMAND.proceedToPrev();
				} else {
					Toast.makeText(getBaseContext(), "No previous input",
							Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});

		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				actionOnNextBtnClick();
			}
		});
		nextButton.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				if (CUR_INPUT_COMMAND != null) {
					skipPages(PagesToSkipOnLongClick, 1);
					CUR_INPUT_COMMAND.proceedToNext();
				} else {
					Toast.makeText(getBaseContext(), "No previous input",
							Toast.LENGTH_SHORT).show();
				}
				return true;
			}
		});

		mainText.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				actionOnNextBtnClick();
				return true;
			}
		});
		//initialize static values
		SurahInformationContainer.loadAllSuraInfos(MainActivity.this);
		FileItemContainer.initializeFileItems(getApplicationContext());
		//to pass the setTextTypeFace when prefs are updated in updatePrefs()
		FontSettingActivity.setDataChanged(true);

		WordInfoLoader.returnToInitialState();

		allQuranTexts = new QuranText[Max_Quran_Texts];
		// loading Quran Text files
		// loadAllFiles();
		loadFonts();
		showARandomAyah=false;

		if (sharedPrefs.getBoolean(getString(R.string.key_showTextSelectOnStart), true)) {
			//when no text or invalid text index has been selected
			updatePrimaryTextIndex();//updatefromprefs() calls it

			showTextSelectionDialog();
			// updateFromPrefs() is included there,
			// so that it will be called after the dialog diappears
			// else it will be updated while the dialog is visible

		} else {
			updateFromPrefs();
			executeOnStartActions();
		}

		// load English text for searching if it's not loaded
		//loadTextFile(English_Text_Index);
		//new TextUpdatingTask(this,"English Text").execute(English_Text_Index);

		// ToBePrintedAyahs=new ArrayList<>();

		setSearchModeOff();

	}

	private void actionOnPrevButtonClick() {
		if (CUR_INPUT_COMMAND != null) {
			CUR_INPUT_COMMAND.proceedToPrev();
		} else {
			Toast.makeText(getBaseContext(), "No previous input",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void actionOnNextBtnClick() {
		if (CUR_INPUT_COMMAND != null) {
			CUR_INPUT_COMMAND.proceedToNext();
		} else {
			Toast.makeText(this, "No previous input.\nSee help for instructions.",
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onBackPressed() {
		if(mplayer!=null){
			try{
				if(mplayer.isPlaying())
					mplayer.stop();
			}catch(IllegalStateException se){}
		}
		if (sharedPrefs.getBoolean(getString(R.string.key_confirmExit), true))
			tryExitApp();
		else
			finish();
	}

	private void tryExitApp() {
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.title_editor_exit)
				.setMessage(R.string.text_exit_confirmation)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}

						}).setNegativeButton("No", null).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		boolean shouldPlayRecitationVisible=false;
		if(isRecitaionAudioOn){
			menu.findItem(R.id.action_recitationAudio).setIcon(R.drawable.ic_volume_off)
			.setTitle(R.string.action_recitationAudio_off);

			shouldPlayRecitationVisible=true;
		}
		if(isInSearchMode){
			menu.findItem(R.id.action_addBookmark).setVisible(false);
			menu.findItem(R.id.action_showSurahList).setVisible(false);
			menu.findItem(R.id.action_searchInText).setIcon(R.drawable.ic_clear_search);

			shouldPlayRecitationVisible=false;
		}
		if(shouldPlayRecitationVisible){
			menu.findItem(R.id.action_playRecitation).setVisible(true);
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// from settings
		if (requestCode == REQUEST_SETTINGS) {
			updateFromPrefs();
		}
		// from sura list
		else if (requestCode == REQUEST_SURAH_LIST && resultCode == RESULT_OK
				&& data != null) {
			int suraNum = data.getIntExtra("sura_num", 0);
			if (suraNum > 0) {
				Ayah ayah = new Ayah(suraNum - 1, 0);
				CUR_INPUT_COMMAND = new InputCommand(ayah, 1);
				mainText.setText("");
				printSingleAyah(ayah);
				// send cursor to the end
				commandText.setSelection(commandText.getText().length());
				if(isInSearchMode){
					setSearchModeOff();
				}
			}
		}

		else if(requestCode == REQUEST_BookmarksDisplay && resultCode == RESULT_OK
				&& data!=null){
			if(isInSearchMode){
				setSearchModeOff();
			}

			int index=data.getIntExtra("index", -1);
			if(index<0){
				CUR_INPUT_COMMAND=getBookmarkInputCommand();
				printAllAyahs();
			}
			else{
				CUR_INPUT_COMMAND = new InputCommand(BookmarkItemContainer.getBookmarkItem(index).getAyah(), 1);
				mainText.setText("");
				printSingleAyah(CUR_INPUT_COMMAND.ayah);
				// send cursor to the end
				commandText.setSelection(commandText.getText().length());
			}
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		if (id == R.id.action_showSurahList) {
			Intent intent = new Intent(this, SuraListActivity.class);
			this.startActivityForResult(intent, REQUEST_SURAH_LIST);
			return true;
		}
		if (id == R.id.action_playRecitation) {
			if(CUR_INPUT_COMMAND!=null){
				if(CUR_INPUT_COMMAND.totalToPrint==1){
					playAnAyah(CUR_INPUT_COMMAND.ayah);
				}
				else if(CUR_INPUT_COMMAND.totalToPrint>1){
					Toast.makeText(this, "Cannot play multiple ayaat",
							Toast.LENGTH_SHORT).show();
				}
			}else{
				Toast.makeText(this, "No input given.\nSee help for instructions.",
						Toast.LENGTH_SHORT).show();
			}
			return true;
		}
		if (id == R.id.action_settings) {
			this.startActivityForResult(SettingsActivity.start(this),
					REQUEST_SETTINGS);
			return true;
		}
		if (id == R.id.action_searchInText) {
			commandText.setText("");
			if (isInSearchMode) {
				setSearchModeOff();
			} else {
				setSearchModeOn();
			}
		}
		if (id == R.id.action_recitationAudio) {
			if (isRecitaionAudioOn) {
				setRecitationAudioOff();
			} else {
				setRecitationAudioOn();
			}

		}
		if (id == R.id.action_addBookmark) {
			if(CUR_INPUT_COMMAND!=null && CUR_INPUT_COMMAND.inputMode==InputMode.MODE_VERSE){
				Intent intent = new Intent(this, BookmarkEditActivity.class);
				Ayah ayah=CUR_INPUT_COMMAND.ayah;
				intent.putExtra("suraIndex", ayah.suraIndex);
				intent.putExtra("ayahIndex", ayah.ayahIndex);

				if(PRIMARY_TEXT_INDEX!=Word_Info_Index){
					intent.putExtra("text", primaryText.getQuranText(ayah));
				}else if(secondaryText!=null){
					intent.putExtra("text", secondaryText.getQuranText(ayah));
				}
				this.startActivity(intent);
			}
			else{
				Toast.makeText(this, "Couldn't get the target ayah. Try in verse mode.",
						Toast.LENGTH_SHORT).show();
			}

			return true;
		}
		if (id == R.id.action_showAllBookmark) {
			Intent intent = new Intent(this, BookmarkDisplayActivity.class);
			this.startActivityForResult(intent, REQUEST_BookmarksDisplay);
			return true;
		}
		if (id == R.id.action_getFullText) {
			Intent intent=new Intent(MainActivity.this,CopyTextActivity.class);
			intent.putExtra("text", mainText.getText().toString());

			this.startActivity(intent);
			return true;
		}
		if (id == R.id.action_additText) {
			Intent intent = new Intent(this, AdditTextActivity.class);
			this.startActivity(intent);
			return true;
		}
		if (id == R.id.action_showInfo) {
			Intent intent = new Intent(this, InfoActivity.class);
			this.startActivity(intent);
			return true;
		}
		if (id == R.id.action_showHelp) {
			Intent intent = new Intent(this, HelpActivity.class);
			this.startActivity(intent);
			return true;
		}
		if (id == R.id.action_tryExit) {
			tryExitApp();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(isRecitaionAudioOn){
			return super.dispatchKeyEvent(event);
		}
		int action = event.getAction();
		int keyCode = event.getKeyCode();
		switch (keyCode) {
		case KeyEvent.KEYCODE_VOLUME_UP:
			if (action == KeyEvent.ACTION_DOWN) {
				actionOnNextBtnClick();
			}
			return true;
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			if (action == KeyEvent.ACTION_DOWN) {
				actionOnPrevButtonClick();
			}
			return true;
		default:
			return super.dispatchKeyEvent(event);
		}
	}

	private InputCommand processInput(String input) {
		input = input.trim();
		if (isInSearchMode) {

			// input string should be of length greater than 1, else Table[1]
			// AIOB exception
			if (input.length() < 2) {
				Toast.makeText(
						this,
						"Searching text should have to be a word "
								+ "containing more than one letter",
						Toast.LENGTH_LONG).show();

				return null;
			}

			int startSuraIndex = 0;
			int endSuraIndex = 113;

			if (input.charAt(0) == '[' && input.indexOf(']') != -1) {
				int indexOfBracketClosed = input.indexOf(']');
				String rangeText = input.substring(1, indexOfBracketClosed);

				// ',','-',':' and space are acceptable
				rangeText = rangeText.replace(",", " ");
				rangeText = rangeText.replace("-", " ");
				rangeText = rangeText.replace(":", " ");
				String parts[] = rangeText.split(" ");

				try {
					startSuraIndex = Integer.parseInt(parts[0]) - 1;// sura no-1
																	// =suraIndex
					endSuraIndex = Integer.parseInt(parts[1]) - 1;

					startSuraIndex %= 114;
					endSuraIndex %= 114;

					if (input.length() > indexOfBracketClosed + 1) {
						input = input.substring(indexOfBracketClosed + 1);
						// remove a formal space from the query string
						if (input.charAt(0) == ' ')
							input = input.substring(1);
					} else {
						Toast.makeText(getBaseContext(),
								"Target String is null", Toast.LENGTH_SHORT)
								.show();

						return null;
					}

				} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
					Toast.makeText(
							getBaseContext(),
							"Couldn't apply ranged search\n"
									+ "See Help for correct searching procedure.",
							Toast.LENGTH_LONG).show();
				}

				if (input.length() < 2) {
					Toast.makeText(
							this,
							"Searching text should have to be a word "
									+ "containing more than one letter",
							Toast.LENGTH_LONG).show();

					return null;
				}

			}

			return new InputCommand(startSuraIndex, endSuraIndex, input);
		}

		// else not in search mode
		// all input strings are as time patter i.e. 2:3:4:5
		// ':','/',' ' and '-' are acceptable .
		input = input.replace('/', ' ');
		input = input.replace(':', ' ');
		input = input.replace('-', ' ');
		String parts[] = input.split(" ");
		if (parts.length < 2) {
			Toast.makeText(this, R.string.text_invalid_input, Toast.LENGTH_SHORT).show();
			return null;
		}

		int suraNo, startAyahNo, endAyahNo;

		try {
			suraNo = Integer.parseInt(parts[0]);
			startAyahNo = Integer.parseInt(parts[1]);
			// default endAyah no. is set to start ayah no. by default
			// else printAllAyah() will give error
			endAyahNo = startAyahNo;

			if (parts.length > 2)
				endAyahNo = Integer.parseInt(parts[2]);
		} catch (NumberFormatException e) {
			Toast.makeText(this, R.string.text_invalid_input, Toast.LENGTH_SHORT).show();
			return null;
		}

		// ayah construction validity is checked inside ayah class
		// no need to recheck here
		if (endAyahNo < startAyahNo) {
			Toast.makeText(this, "Error: start should be smaller than end",
					Toast.LENGTH_SHORT).show();
			return null;
		}

		int totalToPrint = endAyahNo - startAyahNo + 1;
		Ayah inputAyah = new Ayah(suraNo - 1, startAyahNo - 1);

		return new InputCommand(inputAyah, totalToPrint);
	}

	//audio=================
	private void prepareAudioStorageDir(){
		String state=Environment.getExternalStorageState();
		// has writable external  storage
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			audioStorageDir = new File(Environment.getExternalStorageDirectory(),storageFolderName+"/"+AudioStorageDirName);
		} else {
			ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
			audioStorageDir = contextWrapper.getDir(storageFolderName+"/"+AudioStorageDirName,
					Context.MODE_PRIVATE);
		}

		if(!audioStorageDir.exists()){
			audioStorageDir.mkdirs();
		}else{//check if new 10 char file added, if yes then shorten
			new AudioFileFolderizingTask(this).execute();
		}
	}

	private File getMp3File(Ayah ayah){
		File mp3file=new File(audioStorageDir,Integer.toString(ayah.suraIndex+1)+"/"+
				Integer.toString(ayah.ayahIndex+1)+".mp3");

		return mp3file;
	}

	private void playAnAyah(Ayah ayah){
		if(mplayer!=null){
			try{
				if(mplayer.isPlaying())
					mplayer.stop();
			}catch(IllegalStateException se){}
		}
		mplayer.reset();
		File mp3file=getMp3File(ayah);

		try {
			mplayer.setDataSource(mp3file.getPath());
			mplayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					mplayer.start();
				}
			});
			mplayer.prepare();
		}
		catch (IOException e) {
			Toast.makeText(this,"Audio File Not Found in the folder: \""+audioStorageDir.getPath().toString()+"\" . See HELP for instructions.", Toast.LENGTH_LONG).show();
			//e.printStackTrace();
		}
		catch (IllegalStateException e) {
			Toast.makeText(this, "Illegal State", Toast.LENGTH_SHORT).show();
			//e.printStackTrace();
		}
	}

	private void setRecitationAudioOn(){
		isRecitaionAudioOn=true;
		Toast.makeText(this, "Recitation On", Toast.LENGTH_SHORT).show();
		invalidateOptionsMenu();
		if(audioStorageDir==null){
			prepareAudioStorageDir();
		}
		mplayer=new MediaPlayer();
	}

	private void setRecitationAudioOff(){
		isRecitaionAudioOn=false;
		if(mplayer!=null){
			if(mplayer.isPlaying()){
				mplayer.stop();
			}
			mplayer.release();
		}
		Toast.makeText(this, "Recitation Off", Toast.LENGTH_SHORT).show();
		invalidateOptionsMenu();
	}

	class AudioFileFolderizingTask extends AsyncTask<Void, Void, Void>{

		ProgressDialog progressDialog;
		public AudioFileFolderizingTask(Context context) {
			progressDialog=new ProgressDialog(context);
			progressDialog.setIndeterminate(true);
			progressDialog.setMessage("Preparing New Audio Files");
			progressDialog.setCancelable(false);
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			File files[]=audioStorageDir.listFiles();

			for(int i=0;i<files.length;i++){
				if(files[i].isFile()){
					String fileName= files[i].getName();
					//long .mp3 file name , 6char+ ".mp3"=10 chars
					if(fileName.length()==10 && fileName.endsWith(".mp3")){
						int surahNum=0;
						try{
							surahNum=Integer.parseInt(fileName.substring(0,3));//check if it contains all integer digits
						}catch(NumberFormatException ne){
							//ne.printStackTrace();
						}

						if(surahNum>0){
							//moving the file
							File destFileDir=new File(audioStorageDir+"/"+
									Integer.toString(surahNum));

							if(!destFileDir.exists()){
								destFileDir.mkdir();
							}

							int k;
							for(k=3;k<5 && fileName.charAt(k) =='0';k++);
							String ayahFileName=fileName.substring(k);
							File destFile=new File(destFileDir,ayahFileName);

							files[i].renameTo(destFile);
						}
					}
				}
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
		}
	}

	//=====================================
	//TODO
	private void printSingleAyah(Ayah ayah) {

		mainText.setText("");
		commandText.setText(ayah.toString());
		// send cursor to the end
		commandText.setSelection(commandText.getText().length());

		if (PRIMARY_TEXT_INDEX == Word_Info_Index)// word info
		{
			if (!WordInfoLoader.isLoadingCompleted) {
				mainText.append("All data haven't yet been loaded. Please Wait "
						+ "a little and enter your command again.");
			} else {
				mainText.append(getWordInfoText(CUR_INPUT_COMMAND.ayah));
			}

		} else {// other text
			if (primaryText != null)
				mainText.append("[" + ayah.toDetailedString() + "]\n    "
						+ primaryText.getQuranText(ayah)
						+ "\n");
			else {
				mainText.append("Couldn't Load Selected Text File\n");
			}
		}

		if (secondaryText != null) {
			mainText.append("\n[" + ayah.toDetailedString() + "]\n    "
					+ secondaryText.getQuranText(ayah)
					+ "\n");
		}

		scrollToTop();
		if(isRecitaionAudioOn){
			playAnAyah(ayah);//TODO check if looping occurs
		}
	}

	private void printAllAyahs() {
		mainText.setText("");
		if (PRIMARY_TEXT_INDEX == Word_Info_Index)// word info
		{
			if (CUR_INPUT_COMMAND.totalToPrint != 1) {
				Toast.makeText(
						this,
						"Cannot show multiple Ayahs in Word by Word Text Mode. Change the primary text.",
						Toast.LENGTH_SHORT).show();
			} else if (CUR_INPUT_COMMAND.inputMode == InputMode.MODE_VERSE)
				printSingleAyah(CUR_INPUT_COMMAND.ayah);
		} else {// other text
				// single ayah printing mode
				// only one single ayah have to be printed
			if (CUR_INPUT_COMMAND.inputMode == InputMode.MODE_VERSE
					&& CUR_INPUT_COMMAND.totalToPrint == 1) {
				printSingleAyah(CUR_INPUT_COMMAND.ayah);
			} else {

				if (CUR_INPUT_COMMAND.inputMode == InputMode.MODE_VERSE) {
					commandText.setText(CUR_INPUT_COMMAND.ayah.toString()
							+ "-"
							+ Integer.toString(CUR_INPUT_COMMAND.totalToPrint
									+ CUR_INPUT_COMMAND.ayah.ayahIndex));
					// send cursor to the end
					commandText.setSelection(commandText.getText().length());
				}

				runOnUiThread(new AyahPrinterRunnable());
			}
		}
	}

	class AyahPrinterRunnable implements Runnable {

		int printStartIndex;
		int printEndIndex;
		int sizePrintable;
		ArrayList<Ayah> listOfAyahs;

		public AyahPrinterRunnable() {
			sizePrintable = CUR_INPUT_COMMAND.totalToPrint;

			printStartIndex = CUR_INPUT_COMMAND.ayahPrntStartIndex;
			printEndIndex = printStartIndex + MAX_AYAHS_IN_ONE_PAGE - 1;
			if (printEndIndex >= sizePrintable) {
				printEndIndex = sizePrintable - 1;
			}
			listOfAyahs = CUR_INPUT_COMMAND.ToBePrintedAyahs;
		}

		@Override
		public void run() {

			int size = listOfAyahs.size();

			StringBuilder sb = new StringBuilder();

			if (CUR_INPUT_COMMAND.inputMode == InputMode.MODE_SEARCH) {
				sb.append("Searched for: \'" + CUR_INPUT_COMMAND.Query + "\'\n");
				if (size > MAX_SEARCH_COUNT) {
					sb.append("More than " + Integer.toString(MAX_SEARCH_COUNT)
							+ " results found.\n\n");
					sb.append("Showing only " + Integer.toString(sizePrintable)
							+ " result(s) :\n\n");
				} else {
					sb.append("Total of " + Integer.toString(size)
							+ " result(s) found.\n\n");
				}
			}

			//for all modes

			sb.append("This is page "
					+ Integer.toString(printStartIndex / MAX_AYAHS_IN_ONE_PAGE
							+ 1)
					+ " of "
					+ Integer.toString((sizePrintable - 1)
							/ MAX_AYAHS_IN_ONE_PAGE + 1) + " ["
					+ Integer.toString(printEndIndex - printStartIndex + 1)
					+ " ayah(s)]\n\n");

			for (int i = printStartIndex; i <= printEndIndex; i++) {

				Ayah ayah = listOfAyahs.get(i);

				// printing text is primaryText;
				sb.append("[" + ayah.toDetailedString() + "]\n    "
						+ primaryText.getQuranText(ayah)
						+ "\n");

				sb.append("\n\n");

				// Secondary text selected , should not be word by word text
				if (SECONDARY_TEXT_INDEX != Word_Info_Index) {
					sb.append("[" + ayah.toDetailedString() + "]\n    "
							+ secondaryText.getQuranText(ayah) + "\n");

					sb.append("\n\n");
				}
			}

			mainText.append(sb);

			scrollToTop();
		}

	}

	class InputCommand {
		Ayah ayah;
		int totalToPrint;
		InputMode inputMode;
		String Query;

		int ayahPrntStartIndex;

		ArrayList<Ayah> ToBePrintedAyahs;

		//for printing bookmarks
		public InputCommand(ArrayList<Ayah> ayahs){
			this.inputMode = InputMode.MODE_RANDOM;
			ToBePrintedAyahs=ayahs;

			totalToPrint = ToBePrintedAyahs.size();
			ayahPrntStartIndex = 0;
		}

		public InputCommand(Ayah ayah, int ttlToPrint) {
			this.inputMode = InputMode.MODE_VERSE;

			this.ayah = ayah;

			// ####
			ToBePrintedAyahs = new ArrayList<>();

			while ((ttlToPrint--) > 0 && ayah != null) {
				ToBePrintedAyahs.add(ayah);
				ayah = ayah.getNexTAyah();
			}

			totalToPrint = ToBePrintedAyahs.size();
			ayahPrntStartIndex = 0;
		}

		// for search command
		public InputCommand(int startSuraIndex, int endSuraIndex, String query) {

			this.inputMode = InputMode.MODE_SEARCH;
			this.Query = query;

			// ####
			ToBePrintedAyahs=new ArrayList<>();

			if(Search_Operand_Text_Id==Search_Operand_Only_English){
				allQuranTexts[English_Text_Index].search(Query,
						MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
						ToBePrintedAyahs);
			}
			else if(Search_Operand_Text_Id==Search_Operand_Pri_And_Secondary){
				primaryText.search(Query,
						MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
						ToBePrintedAyahs);

				if(ToBePrintedAyahs.size()==0 && secondaryText!=null){
					secondaryText.search(Query,
							MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
							ToBePrintedAyahs);
				}
			}
			else if(Search_Operand_Text_Id==Search_Operand_All){
				allQuranTexts[English_Text_Index].search(Query,
						MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
						ToBePrintedAyahs);

				if(ToBePrintedAyahs.size()==0 && PRIMARY_TEXT_INDEX!=English_Text_Index){
					primaryText.search(Query,
							MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
							ToBePrintedAyahs);
				}

				if(ToBePrintedAyahs.size()==0 && secondaryText!=null
						&& SECONDARY_TEXT_INDEX!=English_Text_Index){
					secondaryText.search(Query,
							MAX_SEARCH_COUNT, startSuraIndex, endSuraIndex,
							ToBePrintedAyahs);
				}
			}

			totalToPrint = ToBePrintedAyahs.size();

			if (totalToPrint > MAX_SEARCH_COUNT) {
				totalToPrint = MAX_SEARCH_COUNT;
			}

			ayahPrntStartIndex = 0;
		}

		public void proceedToPrev() {
			//search mode or random mode
			if (ayahPrntStartIndex - MAX_AYAHS_IN_ONE_PAGE >= 0) {
				ayahPrntStartIndex -= MAX_AYAHS_IN_ONE_PAGE;

				printAllAyahs();
				return;
			}

			if (inputMode == InputMode.MODE_VERSE) {
				// Enabling single ayah printing mode
				if (totalToPrint != 1) {
					totalToPrint = 1;
					// clearing unnecessary items
					ToBePrintedAyahs.clear();
				}

				Ayah prevAyah = ayah.getPrevAyah();
				if (prevAyah != null) {
					ayah = prevAyah;

					printSingleAyah(CUR_INPUT_COMMAND.ayah);
				} else
					Toast.makeText(getBaseContext(), "Reached to first ayah.",
							Toast.LENGTH_SHORT).show();
			}
			// else if(inputMode==InputMode.MODE_SEARCH){
			else {
				Toast.makeText(getBaseContext(), "Reached to first page.",
						Toast.LENGTH_SHORT).show();
			}

		}

		public void proceedToNext() {
			//search mode or random mode
			if (ayahPrntStartIndex < totalToPrint - MAX_AYAHS_IN_ONE_PAGE) {
				ayahPrntStartIndex += MAX_AYAHS_IN_ONE_PAGE;

				printAllAyahs();
				return;
			}
			if (inputMode == InputMode.MODE_VERSE) {

				// Enabling single ayah printing mode
				if (totalToPrint != 1) {
					// current ayah is last ayah at ToBePrintedAyahs
					ayah = ToBePrintedAyahs.get(totalToPrint - 1);
					totalToPrint = 1;
					// clearing unnecessary items
					ToBePrintedAyahs.clear();
				}

				Ayah nextAyah = ayah.getNexTAyah();
				if (nextAyah != null) {
					ayah = nextAyah;

					printSingleAyah(CUR_INPUT_COMMAND.ayah);
				} else
					Toast.makeText(getBaseContext(), "Reached to last ayah.",
							Toast.LENGTH_SHORT).show();
			}// else if(inputMode==InputMode.MODE_SEARCH){
			else {
				Toast.makeText(getBaseContext(), "Reached to last page.",
						Toast.LENGTH_SHORT).show();
			}
		}

	}

	private InputCommand getBookmarkInputCommand(){

		if(BookmarkItemContainer.getBookmarkItemsSize()==0){
			return new InputCommand(new Ayah(0,0), 1);
		}

		ArrayList<Ayah> ayahs=new ArrayList<>();
		for(int i=0,j=BookmarkItemContainer.getBookmarkItemsSize();i<j;i++){
			ayahs.add(BookmarkItemContainer.getBookmarkItem(i).getAyah());
		}

		return new InputCommand(ayahs);
	}

	private void hideSoftKeyBoard() {
		imm.hideSoftInputFromWindow(commandText.getWindowToken(), 0);
	}

	private void loadTextFile(int index) {
		if (index == Word_Info_Index)// wordInfo
		{ // TODO find more memory efficient code
			// arabic text also should have to be loaded;
			Log.i("call", "called load quranText");
			loadTextFile(Arabic_Text_Index);// load arabic text

			if (WordInfoLoader.isInfoLoaded) {
				// already loaded or being loaded
				// do nothing
			} else {

				new WordInfoLoader().load(getBaseContext());

			}

			return;
		}

		//not loaded yet
		if (allQuranTexts[index] == null) {

			boolean isArabic = false;
			if (index == Arabic_Text_Index)
				isArabic = true;
			Log.d("init", "loading file, index: " + index);

			if (index < Total_Default_Quran_Texts) {
				InputStream in = getResources()
						.openRawResource(resourceIDs[index]);
				allQuranTexts[index] = new QuranText(in, isArabic);

			} else {
				InputStream in = null;
				try {
					in = new FileInputStream(FileItemContainer
							.getFileItem(index-Total_Default_Quran_Texts).getFile());
				} catch (IOException ie) {
					//ie.printStackTrace();
				}
				allQuranTexts[index] = new QuranText(in, false);
			}

			Log.i("success", "Text loading complete");
		}
	}

	/*
	 * private void lowerVersionAlert(){ Toast.makeText(getApplicationContext(),
	 * "Not available in this version.", Toast.LENGTH_SHORT).show(); }
	 */

	private void loadFonts() {

		defaultTypefaces = new ArrayList<>();
		defaultTypefaces.add(Typeface.DEFAULT);
		for(int i=1;i<defaultTypefaceNames.length;i++){
			defaultTypefaces.add(Typeface.createFromAsset(getAssets(),
				"fonts/"+defaultTypefaceNames[i]));
		}
	}

	private static List<WordInformation> getInfoOfWords(Ayah ayah) {
		// index of first ayah of the sura in all ayah sets
		int indexOfFirstAyah = SurahInformationContainer.totalAyahsUpto(ayah.suraIndex);
		int indexOfSelectedAyah = indexOfFirstAyah + ayah.ayahIndex;
		// address in the info list of that selected ayah
		// index of the first word of this ayah
		int indxOfFirstWord = WordInfoLoader.startIndexOfAyah
				.get(indexOfSelectedAyah);
		// index of the first word of the next ayah
		int indxOfFWNextA = WordInfoLoader.startIndexOfAyah
				.get(indexOfSelectedAyah + 1);

		// now listing all the word informations
		List<WordInformation> wordsOfAyah = new ArrayList<>();
		// List<Image>images=new ArrayList<>();

		for (int i = indxOfFirstWord; i < indxOfFWNextA; i++) {
			wordsOfAyah.add(WordInfoLoader.infoWords.get(i));
			// images.add(ImageLoader.getImageFromFile(i));
		}

		return wordsOfAyah;

	}

	private static String getWordInfoText(Ayah ayah) {
		String arabicAyahTextWords[] = allQuranTexts[Arabic_Text_Index].getQuranText(ayah)
				.split(" ");
		List<WordInformation> wordsOfAyah = getInfoOfWords(ayah);
		int wordIndexToDisplay = 0;
		int wordInfoIndexToDisplay = 0;

		String text = "[" + ayah.toDetailedString() + "]\n\n";
		while (wordIndexToDisplay < arabicAyahTextWords.length) {

			// arabic text
			text += arabicAyahTextWords[wordIndexToDisplay];
			// waqf checking
			int k = arabicAyahTextWords[wordIndexToDisplay].charAt(0);

			if ((k >= '\u0610' && k <= '\u0615')
					|| (k >= '\u06D6' && k <= '\u06ED')) {
				wordIndexToDisplay++;
				text += "\n--[waqf]--\n\n";
				continue;
			}
			// waqf checked

			// transliteration and meaning
			if (wordInfoIndexToDisplay < wordsOfAyah.size()) {
				text += "\n["
						+ wordsOfAyah.get(wordInfoIndexToDisplay).transliteration
						+ "]" + "\n"
						+ wordsOfAyah.get(wordInfoIndexToDisplay).meaning;
			} else {
				text += "\n(missing word meaning)";// not found
			}

			text += "\n\n";

			wordInfoIndexToDisplay++;
			wordIndexToDisplay++;
		}

		text += "\n==========\n";
		return text;
	}

	private void scrollToTop() {
		scrollView.post(new Runnable() {
			public void run() {
				scrollView.fullScroll(View.FOCUS_UP);
			}
		});
	}

	private void setSearchModeOn() {
		if(PRIMARY_TEXT_INDEX==Word_Info_Index){
			Toast.makeText(this, "Searching Cannot Be Done In Word By Word Text Mode."
					+ "\nChange Primary Text.", Toast.LENGTH_LONG).show();
			return;

		}
		if((Search_Operand_Text_Id==Search_Operand_Only_English
				|| Search_Operand_Text_Id==Search_Operand_All)
				&& allQuranTexts[English_Text_Index]==null){
			new TextUpdatingTask(this,"English Text").execute(English_Text_Index);
			//update text info, that is, search operand text is allQText[english text index]
		}
		isInSearchMode = true;
		commandText.setInputType(InputType.TYPE_CLASS_TEXT);
		commandText.setHint(R.string.hint_commandTextToSearch);

		invalidateOptionsMenu();
	}

	private void setSearchModeOff() {
		isInSearchMode = false;
		commandText.setInputType(InputType.TYPE_CLASS_DATETIME);
		commandText.setHint(R.string.hint_commandText);

		invalidateOptionsMenu();
	}

	private void showTextSelectionDialog() {
		AlertDialog.Builder textSelectorBuilder = new AlertDialog.Builder(this);

		PRIMARY_TEXT_INDEX = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_primary_text_selection), Integer.toString(English_Text_Index)));

		final SharedPreferences.Editor editor = sharedPrefs.edit();
		final int newSelectedIndex[] = { PRIMARY_TEXT_INDEX };
		// one int element array to assign value from inner class

		textSelectorBuilder.setTitle(R.string.title_textSelection);

		String defaultItems[]=getResources().getStringArray(R.array.array_textSelection);
		int defaultItemSize= defaultItems.length;

		/*defaultItemSize= defaultItemSize <= Total_Default_Quran_Texts?
					defaultItemSize :Total_Default_Quran_Texts;*/

		int additinalItemSize=FileItemContainer.getFileItemsSize();

		String allItems[]=new String[defaultItemSize+
		                             additinalItemSize];

		for(int i=0;i<defaultItemSize;i++){
			allItems[i]=defaultItems[i];
		}
		for(int i=defaultItemSize,j=0;j<additinalItemSize;i++,j++){
			allItems[i]=FileItemContainer.getFileItem(j).getFileAliasName();
		}

		textSelectorBuilder.setSingleChoiceItems(allItems,
				PRIMARY_TEXT_INDEX, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int whichIndex) {
						newSelectedIndex[0] = whichIndex;
					}
				});

		textSelectorBuilder.setPositiveButton("Done",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						if (newSelectedIndex[0] != PRIMARY_TEXT_INDEX) {
							editor.putString(getString(R.string.key_primary_text_selection),
									Integer.toString(newSelectedIndex[0]));
							editor.commit();
						}
					}
				});
		textSelectorBuilder.setNegativeButton("Don't Show This Again",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						editor.putBoolean(getString(R.string.key_showTextSelectOnStart), false);
						if (newSelectedIndex[0] != PRIMARY_TEXT_INDEX) {
							editor.putString(getString(R.string.key_primary_text_selection),
									Integer.toString(newSelectedIndex[0]));
						}

						editor.commit();
					}
				});

		AlertDialog dialog=textSelectorBuilder.create();

		dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				updateFromPrefs();
				executeOnStartActions();
			}
		});

		dialog.show();
	}

	private void skipPages(int pages, int direction) {

		int skippedAyah = (pages - 1) * MAX_AYAHS_IN_ONE_PAGE;
		// as proceed to next or proceed to prev is called
		// one page is automatically skipped, so (page-1)

		int startIndex;

		if (direction > 0)
			startIndex = CUR_INPUT_COMMAND.ayahPrntStartIndex + skippedAyah;

		else
			startIndex = CUR_INPUT_COMMAND.ayahPrntStartIndex - skippedAyah;

		if (startIndex < 0 || startIndex >= CUR_INPUT_COMMAND.totalToPrint) {
			Toast.makeText(this, "Failed to Skip pages", Toast.LENGTH_SHORT)
					.show();
		} else {
			CUR_INPUT_COMMAND.ayahPrntStartIndex = startIndex;
		}
	}

	/*
	 * private void showScndryTxtSltctnDialog() { AlertDialog.Builder builder =
	 * new AlertDialog.Builder(this);
	 *
	 * final int selectedIndex[] = { -1 }; // one int element array to assign
	 * value from inner class
	 *
	 * builder.setTitle(R.string.title_scdndryTxtSelection);
	 *
	 * final String
	 * items[]=getResources().getStringArray(R.array.array_textSelection);
	 * items[PRIMARY_TEXT_INDEX]="Primary Text";
	 * of word by word items[items.length-1]="No Secondary Text";
	 *
	 * if(SECONDARY_TEXT_INDEX==-1){ SECONDARY_TEXT_INDEX=items.length-1; }
	 *
	 * builder.setSingleChoiceItems(items, SECONDARY_TEXT_INDEX, new
	 * DialogInterface.OnClickListener() {
	 *
	 * @Override public void onClick(DialogInterface dialog, int whichIndex) {
	 * selectedIndex[0] = whichIndex; } });
	 *
	 * builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
	 * public void onClick(DialogInterface dialog, int id) { //not equal to
	 * primary text index if (selectedIndex[0] != PRIMARY_TEXT_INDEX) {
	 * SECONDARY_TEXT_INDEX=selectedIndex[0]; //if "No Secondary Text"
	 * if(SECONDARY_TEXT_INDEX==items.length-1){ SECONDARY_TEXT_INDEX=-1; }else{
	 * loadTextFile(SECONDARY_TEXT_INDEX); } }else{
	 * Toast.makeText(getBaseContext(),
	 * "Primary Text cannot be Selected as Secondary",
	 * Toast.LENGTH_LONG).show();
	 *
	 * SECONDARY_TEXT_INDEX=-1; } } }); builder.setNegativeButton("Cancel", new
	 * DialogInterface.OnClickListener() { public void onClick(DialogInterface
	 * dialog, int id) { //do nothing } });
	 *
	 * builder.create().show(); }
	 */

	class TextUpdatingTask extends AsyncTask<Integer, Void, Void>{

		ProgressDialog progressDialog;
		public TextUpdatingTask(Context context, String textName) {
			Log.d("new task textName",textName);
			progressDialog=new ProgressDialog(context);
			progressDialog.setIndeterminate(true);
			progressDialog.setMessage("Loading "+textName);
			progressDialog.setCancelable(false);

			//progressDialog.setCancelable(false);
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}
		@Override
		protected Void doInBackground(Integer... indices) {
			Log.d("indices[0]", Integer.toString(indices[0]));
			loadTextFile(indices[0]);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			updateTextInfo();
			tryPrintARandomAyah();
		}
	}

	private synchronized void tryPrintARandomAyah(){
		if(!showARandomAyah){
			notify();
			return;
		}
		if(SECONDARY_TEXT_INDEX != Word_Info_Index){//secondaryIndex in not none
			if(secondaryText==null){//secondaryText is not loaded yet
				notify();
				return;
			}
			//else (secondary text loaded), go to next
		}
		//secondary text already loaded or it is none
		if(primaryText!=null || (PRIMARY_TEXT_INDEX==Word_Info_Index &&
				WordInfoLoader.isLoadingCompleted)){

			CUR_INPUT_COMMAND = new InputCommand(getARandomAyah(), 1);
			mainText.setText("");

			printSingleAyah(CUR_INPUT_COMMAND.ayah);

			showARandomAyah=false;
		}
		notify();
	}

	// TODO, this is a marker for update pref function
	private void updateFromPrefs() {
		mainText.setTextSize(Float.parseFloat(sharedPrefs.getString(
				getString(R.string.key_fontSize), "18")));

		int previousPrimaryIndex = PRIMARY_TEXT_INDEX;

		updatePrimaryTextIndex();//but not loaded

		if (previousPrimaryIndex == Word_Info_Index
				&& previousPrimaryIndex != PRIMARY_TEXT_INDEX) {
			// clearing up a huge memory (word infos)
			WordInfoLoader.returnToInitialState();
			System.gc();
		}

		updateSecondaryTextIndex();// and loaded in case
		//So, secondartyText is initialized first

		MAX_SEARCH_COUNT = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_maxSearchCount), "5000"));

		MAX_AYAHS_IN_ONE_PAGE = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_maxAyahInPage), "10"));

		PagesToSkipOnLongClick = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_pagesToSkipOnClick), "10"));

		//loadTextFile(PRIMARY_TEXT_INDEX);
		//updateTextInfo()
		new TextUpdatingTask(this,"Primary Text").execute(PRIMARY_TEXT_INDEX);

		updateSearchOperandTextIndex();

		setTextTypeface();

		// set gravity
		if (PRIMARY_TEXT_INDEX == Word_Info_Index)// word info
		{
			mainText.setGravity(Gravity.CENTER);
		} else {
			mainText.setGravity(Gravity.TOP);
		}

	}

	private void updatePrimaryTextIndex(){
		PRIMARY_TEXT_INDEX = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_primary_text_selection), Integer.toString(English_Text_Index)));

		//if index is greater than available indices
		if(PRIMARY_TEXT_INDEX>
			(Total_Default_Quran_Texts+FileItemContainer.getFileItemsSize()-1)){

			storePrimaryTextToDefault();
		}

	}
	private void updateSecondaryTextIndex(){
		// default value is "No Secondary Text" that is 0 or -1
		SECONDARY_TEXT_INDEX = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_secondary_text_selection), Integer.toString(Word_Info_Index)));

		if(SECONDARY_TEXT_INDEX==Word_Info_Index)//no secondary text
		{
			return;
		}
		if (SECONDARY_TEXT_INDEX == PRIMARY_TEXT_INDEX) {
			Toast.makeText(
					this,
					"Primary Text and Secondary Text are same."
							+ "\nSecondary Text Disabled.", Toast.LENGTH_LONG)
					.show();

			storeSecondaryTextToDefault();

		} else if (SECONDARY_TEXT_INDEX > Total_Default_Quran_Texts
				+ FileItemContainer.getFileItemsSize() - 1) {

			storeSecondaryTextToDefault();
		} else if(allQuranTexts[SECONDARY_TEXT_INDEX]==null){//No secondary Text
			// loadTextFile(SECONDARY_TEXT_INDEX);
			//updateTextInfo()
			new TextUpdatingTask(this, "Secondary Text")
					.execute(SECONDARY_TEXT_INDEX);
		}

	}

	private void updateSearchOperandTextIndex(){
		Search_Operand_Text_Id = Integer.parseInt(sharedPrefs.getString(
				getString(R.string.key_searchOperandTextIndex), Integer.toString(Search_Operand_All)));
	}
	/*
	 * strore secondary text index as Word Info index
	 */
	private void storeSecondaryTextToDefault(){
		SECONDARY_TEXT_INDEX=Word_Info_Index;
		// save to pref
		SharedPreferences.Editor editor = sharedPrefs.edit();
		editor.putString(getString(R.string.key_secondary_text_selection),
				Integer.toString(Word_Info_Index));// No secondary Text
		editor.commit();
	}

	private void storePrimaryTextToDefault(){
		PRIMARY_TEXT_INDEX=English_Text_Index;

		SharedPreferences.Editor editor = sharedPrefs.edit();
		editor.putString(getString(R.string.key_primary_text_selection),
				Integer.toString(English_Text_Index));
		editor.commit();
	}

	private void updateTextInfo(){
		primaryText = allQuranTexts[PRIMARY_TEXT_INDEX];
		if (SECONDARY_TEXT_INDEX != Word_Info_Index)// Secondary_TEXT_INDEX != <None>
			secondaryText = allQuranTexts[SECONDARY_TEXT_INDEX];
		else
			secondaryText = null;
	}

	//set font
	private void setTextTypeface(){

		//no change has been made
		if(!FontSettingActivity.isDataChanged()){
			return;
		}

		if(FontItemContainer.getAllFontFiles()==null){
			//Initialize from predefined storage location
			FontItemContainer.initializeFontFiles(MainActivity.this);
		}
		/*if (PRIMARY_TEXT_INDEX == Word_Info_Index
				|| PRIMARY_TEXT_INDEX == Arabic_Text_Index) {
			mainText.setTypeface(defaultTypefaces[1]);
		} else {
			mainText.setTypeface(Typeface.DEFAULT);
		}*/

		if(FontItemContainer.getSelectedFontName(PRIMARY_TEXT_INDEX).equals(
				FontItemContainer.Default_Font_File_Name))
		{
			mainText.setTypeface(Typeface.DEFAULT);
			return;
		}

		int fontFileIndex=FontItemContainer.getFontFileIndexInAsset(PRIMARY_TEXT_INDEX);

		if(fontFileIndex!=-1){//file found in asset
			mainText.setTypeface(defaultTypefaces.get(fontFileIndex));
		}
		else{//file not found in asset
			fontFileIndex=FontItemContainer.getFontFileIndexInFiles(PRIMARY_TEXT_INDEX);

			if(fontFileIndex!=-1){//file found in storage
				Typeface typeface=Typeface.createFromFile(FontItemContainer.getFontFile(fontFileIndex));
				mainText.setTypeface(typeface);
			}
			else{//file not found in storage
				mainText.setTypeface(Typeface.DEFAULT);
				FontItemContainer.setSelectedFontName(PRIMARY_TEXT_INDEX,
						FontItemContainer.Default_Font_File_Name);
			}
		}
	}

	public static int getTotalDefaultTypefaces(){
		return defaultTypefaceNames.length;
	}
	public static String getDefaultTypefaceName(int index){
		return defaultTypefaceNames[index];
	}
	public static Typeface getDefaultTypeface(int index){
		return defaultTypefaces.get(index);
	}

	public static Typeface getMainTextTypeface(){
		return mainText.getTypeface();
	}

	public static String getAyahText(Ayah ayah){
		String text="";
		if (PRIMARY_TEXT_INDEX == Word_Info_Index)// word info
		{
			text+=getWordInfoText(ayah);

		} else {// other text
			if (primaryText != null){
				text+="\n[" + ayah.toDetailedString() + "]\n    "
						+ primaryText.getQuranText(ayah)
						+ "\n";
			}
		}

		if (secondaryText != null) {
			text+="\n[" + ayah.toDetailedString() + "]\n    "
					+ secondaryText.getQuranText(ayah)
					+ "\n";
		}

		return text;
	}

	private void executeOnStartActions(){
		int actionIndexOnStart=Integer.parseInt(sharedPrefs.getString(getString(R.string.key_prefActionOnStart),
				Integer.toString(ActionOnStartRandomAyah)));

		if(actionIndexOnStart==ActionOnStartNone){
			return;
		}
		else if(actionIndexOnStart==ActionOnStartSurahList){
			Intent intent = new Intent(MainActivity.this, SuraListActivity.class);
			startActivityForResult(intent, REQUEST_SURAH_LIST);
		}
		else if(actionIndexOnStart==ActionOnStartRandomAyah){
			showARandomAyah=true;
		}
	}

	private Ayah getARandomAyah(){
		Random generator=new Random();
		int surahIndex=generator.nextInt(114);
		int ayahIndex=generator.nextInt(SurahInformationContainer.totalAyahs[surahIndex]);
		/*int allTotalAyahs=SurahInformationContainer.getTotalAyahsUptoSurah114();
		int abstractAyahIndex=generator.nextInt(allTotalAyahs);

		int surahIndex=0;
		int ayahIndex=0;
		int totalAyahsUptoSurahIndex=SurahInformationContainer.totalAyahs[surahIndex];

		for(int i=0;i<113;i++){
			if(abstractAyahIndex<totalAyahsUptoSurahIndex){
				break;
			}
			ayahIndex=totalAyahsUptoSurahIndex;

			surahIndex++;
			totalAyahsUptoSurahIndex+=SurahInformationContainer.totalAyahs[surahIndex];
		}

		ayahIndex=abstractAyahIndex-ayahIndex;*/

		return new Ayah(surahIndex, ayahIndex);
	}
}


text: Admin interface for Dukecon - update room availablility (for now)

redpajama_set_name: RedPajamaGithub


text:
static MagickBooleanType IsPKM(const unsigned char *magick, const size_t length)
{
	if (length < 16)
	{
		return(MagickFalse);
	}
	if (LocaleNCompare((char *)magick, "PKM ", 4) == 0)
	{
		return(MagickTrue);
	}
	return(MagickFalse);
}

static Image *ReadPKMImage(
  const ImageInfo *image_info, ExceptionInfo *exception)
{
	return ReadPKMImage_inner(image_info, exception, MagickFalse);
}

static MagickBooleanType
  WritePKMImage(const ImageInfo *image_info, Image *image)
{
	return WritePKMImage_inner(image_info, image, MagickFalse);
}

static Image *ReadPKMAImage(
	const ImageInfo *image_info, ExceptionInfo *exception)
{
	return ReadPKMImage_inner(image_info, exception, MagickTrue);
}

static MagickBooleanType
WritePKMAImage(const ImageInfo *image_info, Image *image)
{
	return WritePKMImage_inner(image_info, image, MagickTrue);
}

ModuleExport unsigned long RegisterPKMImage(void)
{
	MagickInfo *entry;

	entry = SetMagickInfo("PKM");
	entry->encoder = (EncodeImageHandler *)WritePKMImage;
	entry->decoder = (DecodeImageHandler *)ReadPKMImage;
	entry->magick = (IsImageFormatHandler *)IsPKM;
	entry->description = ConstantString("PKM");
	entry->module = ConstantString("PKM");

	(void)RegisterMagickInfo(entry);

	entry = SetMagickInfo("PKMA");
	entry->encoder = (EncodeImageHandler *)WritePKMAImage;
	entry->decoder = (DecodeImageHandler *)ReadPKMAImage;
	entry->magick = (IsImageFormatHandler *)IsPKM;
	entry->description = ConstantString("PKM_alpha");
	entry->module = ConstantString("PKM");

	(void)RegisterMagickInfo(entry);
	return (MagickImageCoderSignature);
}

ModuleExport void UnregisterPKMImage(void)
{
	(void)UnregisterMagickInfo("PKM");
}


text:
//
//  HLSFloat.h
//  CoconutKit
//
//  Created by Samuel Défago on 9/19/10.
//  Copyright 2010 Hortis. All rights reserved.
//

// Default distance used by macros without distance parameter
#define HLSFloatDefaultMaxDist              5

/**
 * Macros for comparing float or double values
 */
#define floatle_dist(x, y, maxDist)         ((x) < (y) || floateq_dist((x), (y), (maxDist)))
#define floatge_dist(x, y, maxDist)         ((x) > (y) || floateq_dist((x), (y), (maxDist)))
#define floatlt_dist(x, y, maxDist)         ! floatge_dist((x), (y), (maxDist))
#define floatgt_dist(x, y, maxDist)         ! floatle_dist((x), (y), (maxDist))

#define doublele_dist(x, y, maxDist)        ((x) < (y) || doubleeq_dist((x), (y), (maxDist)))
#define doublege_dist(x, y, maxDist)        ((x) > (y) || doubleeq_dist((x), (y), (maxDist)))
#define doublelt_dist(x, y, maxDist)        ! doublege_dist((x), (y), (maxDist))
#define doublegt_dist(x, y, maxDist)        ! doublele_dist((x), (y), (maxDist))

#define floateq(x, y)                       floateq_dist((x), (y), HLSFloatDefaultMaxDist)
#define doubleeq(x, y)                      doubleeq_dist((x), (y), HLSFloatDefaultMaxDist)

#define floatle(x, y)                       floatle_dist((x), (y), HLSFloatDefaultMaxDist)
#define floatge(x, y)                       floatge_dist((x), (y), HLSFloatDefaultMaxDist)
#define floatlt(x, y)                       floatlt_dist((x), (y), HLSFloatDefaultMaxDist)
#define floatgt(x, y)                       floatgt_dist((x), (y), HLSFloatDefaultMaxDist)

#define doublele(x, y)                      doublele_dist((x), (y), HLSFloatDefaultMaxDist)
#define doublege(x, y)                      doublege_dist((x), (y), HLSFloatDefaultMaxDist)
#define doublelt(x, y)                      doublelt_dist((x), (y), HLSFloatDefaultMaxDist)
#define doublegt(x, y)                      doublegt_dist((x), (y), HLSFloatDefaultMaxDist)

#define floatmin(x, y)                      floatmin_dist((x), (y), HLSFloatDefaultMaxDist)
#define floatmax(x, y)                      floatmax_dist((x), (y), HLSFloatDefaultMaxDist)

#define doublemin(x, y)                     doublemin_dist((x), (y), HLSFloatDefaultMaxDist)
#define doublemax(x, y)                     doublemax_dist((x), (y), HLSFloatDefaultMaxDist)

/**
 * Comparison function for floating point numbers. The parameter maxDist is the maximum distance between two (discrete)
 * numbers so that they can be considered to be equal
 */
BOOL floateq_dist(float x, float y, uint32_t maxDist);
BOOL doubleeq_dist(double x, double y, uint64_t maxDist);

/**
 * Max / min functions (not macros; this would have provided weaker type-checking)
 */
float floatmin_dist(float x, float y, uint32_t maxDist);
float floatmax_dist(float x, float y, uint32_t maxDist);
double doublemin_dist(double x, double y, uint64_t maxDist);
double doublemax_dist(double x, double y, uint64_t maxDist);


text:
import log4js = require("log4js");

var loggerConfig = require("../log4js-config.json");
log4js.configure(loggerConfig, {});

class Logger {
    constructor(private logger: log4js.Logger) { }

    debug(message: string, ...args: any[]): void {
        this.logger.debug(message, args);
    }

    info(message: string, ...args: any[]): void {
        this.logger.info(message, args);
    }

    warn(message: string, ...args: any[]): void {
        this.logger.warn(message, args);
    }

    error(message: string, ...args: any[]): void {
        this.logger.error(message, args);
    }
}

export function getLogger() {
    return new Logger(log4js.getLogger());
}


text:
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.editor.language.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.ScriptTask;
import org.junit.Test;

public class TextAnnotationConverterTest extends AbstractConverterTest {

    @Test
    public void convertXMLToModel() throws Exception {
        BpmnModel bpmnModel = readXMLFile();
        validateModel(bpmnModel);
    }

    @Test
    public void convertModelToXML() throws Exception {
        BpmnModel bpmnModel = readXMLFile();
        BpmnModel parsedModel = exportAndReadXMLFile(bpmnModel);
        validateModel(parsedModel);
    }

    @Override
    protected String getResource() {
        return "parsing_error_on_extension_elements.bpmn";
    }

    private void validateModel(BpmnModel model) {
        FlowElement flowElement = model.getFlowElement("_5");
        assertNotNull(flowElement);
        assertTrue(flowElement instanceof ScriptTask);
        assertEquals("_5", flowElement.getId());
        ScriptTask scriptTask = (ScriptTask) flowElement;
        assertEquals("_5", scriptTask.getId());
        assertEquals("Send Hello Message", scriptTask.getName());
    }
}


text:
/**
 * @file
 * @brief System/hardware module for Nordic Semiconductor nRF91 family processor
 *
 * This module provides routines to initialize and support board-level hardware
 * for the Nordic Semiconductor nRF91 family processor.
 */

#include <kernel.h>
#include <init.h>
#include <cortex_m/exc.h>
#include <nrfx.h>
#include <soc/nrfx_coredep.h>
#include <logging/log.h>

#ifdef CONFIG_RUNTIME_NMI
extern void z_NmiInit(void);
#define NMI_INIT() z_NmiInit()
#else
#define NMI_INIT()
#endif

#if defined(CONFIG_SOC_NRF9160)
#include <system_nrf9160.h>
#else
#error "Unknown SoC."
#endif

#define LOG_LEVEL CONFIG_SOC_LOG_LEVEL
LOG_MODULE_REGISTER(soc);

static int nordicsemi_nrf91_init(struct device *arg)
{
	u32_t key;

	ARG_UNUSED(arg);

	key = irq_lock();

#ifdef CONFIG_NRF_ENABLE_ICACHE
	/* Enable the instruction cache */
	NRF_NVMC->ICACHECNF = NVMC_ICACHECNF_CACHEEN_Msk;
#endif

	/* Install default handler that simply resets the CPU
	* if configured in the kernel, NOP otherwise
	*/
	NMI_INIT();

	irq_unlock(key);

	return 0;
}

void z_arch_busy_wait(u32_t time_us)
{
	nrfx_coredep_delay_us(time_us);
}

void z_platform_init(void)
{
	SystemInit();
}

SYS_INIT(nordicsemi_nrf91_init, PRE_KERNEL_1, 0);


text:
package org.xtreemfs.mrc.database.babudb;

import org.xtreemfs.babudb.api.database.DatabaseRequestListener;
import org.xtreemfs.babudb.api.exception.BabuDBException;
import org.xtreemfs.mrc.database.DBAccessResultListener;

/**
 * @author stender
 * @parma <T>
 */
public class BabuDBRequestListenerWrapper<T> implements DatabaseRequestListener<T> {

    private DBAccessResultListener<T> listener;

    public BabuDBRequestListenerWrapper(DBAccessResultListener<T> listener) {
        this.listener = listener;
    }

    /*
     * (non-Javadoc)
     * @see org.xtreemfs.babudb.BabuDBRequestListener#failed(org.xtreemfs.babudb.BabuDBException, java.lang.Object)
     */
    @Override
    public void failed(BabuDBException arg0, Object arg1) {
        listener.failed(arg0,arg1);
    }

    /*
     * (non-Javadoc)
     * @see org.xtreemfs.babudb.BabuDBRequestListener#finished(java.lang.Object, java.lang.Object)
     */
    @Override
    public void finished(T arg0, Object arg1) {
        listener.finished(arg0, arg1);
    }
}


text:
import Common

main :: IO ()
main = run "ghci"


text:
<?php

/**
 * Oh bother!
 *
 * @package bbPress
 * @subpackage Theme
 */

?>

<?php _e( 'Oh bother! No forums were found here!', 'firmasite' ); ?>