package com.teamNikAml.doctorpatient.fragments;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.teamNikAml.doctorpatient.activity.R;


/*import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;*/

public class BackupFragment extends Fragment {

	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_backup, container,
				false);

		try {
	        File sd = Environment.getExternalStorageDirectory();
	        File data = Environment.getDataDirectory();

	        if (data.canWrite()) {
	            String currentDBPath = "//data//"+ "com.teamNikAml.doctorpatient.activity" +"//databases//"+"PatientRecordsDatabase";
	            String backupDBPath = "DatabaseBackup";
	            File currentDB = new File(data, currentDBPath);
	            File backupDB = new File(data, backupDBPath);

	            if (currentDB.exists()) {
	                FileInputStream fileInputStream = new FileInputStream(currentDB);
					FileChannel src = fileInputStream.getChannel();
	                FileOutputStream fileOutputStream = new FileOutputStream(backupDB);
					FileChannel dst = fileOutputStream.getChannel();
	                dst.transferFrom(src, 0, src.size());
	                src.close();
	                dst.close();
	                fileInputStream.close();
	                fileOutputStream.close();
	            }
	        }
	    } catch (Exception e) {
	    }
		return rootView;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
/*
	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnectionSuspended(int arg0) {
		// TODO Auto-generated method stub
		
	}*/
	
	
}

@@ -426,16 +426,77 @@
 vity.R;%0A
+import com.teamNikAml.doctorpatient.database.DatabaseConfig;%0A
 %0A%0A/*impo
@@ -972,16 +972,62 @@
 %09%09try %7B%0A
+%09%09%09%0A%09%09%09System.out.println(%22dfsdfsddsds%22);%0A%09%09%09%0A
 %09       
@@ -1144,33 +1144,8 @@
     
-   if (data.canWrite()) %7B
 %0A%09  
@@ -1154,22 +1154,20 @@
         
-String
+File
  current
@@ -1179,77 +1179,62 @@
 h = 
-%22//data//%22+ %22com.teamNikAml.doctorpatient.activity%22 +%22//d
+getActivity().getApplicationContext().getD
 atabase
-s//%22+
+Path(
 %22Pat
@@ -1253,16 +1253,17 @@
 atabase%22
+)
 ;%0A%09     
@@ -1273,29 +1273,138 @@
     
-String backupDBPath =
+%0A%09            File backupDBPath ;%0A%09            backupDBPath = new File(getActivity().getApplicationContext().getFilesDir() + %22/%22 +
  %22Da
@@ -1416,31 +1416,155 @@
 eBackup%22
+ )
 ;%0A%09    
-        
+%09%09%0A%09            %0A%09            %0A%09            %0A%09            System.out.println(currentDBPath.getPath());%0A%09          %0A%09            //
 File cur
@@ -1607,32 +1607,34 @@
 h);%0A%09           
+//
  File backupDB =
@@ -1691,16 +1691,20 @@
 urrentDB
+Path
 .exists(
@@ -1788,16 +1788,20 @@
 urrentDB
+Path
 );%0A%09%09%09%09%09
@@ -1930,16 +1930,20 @@
 backupDB
+Path
 );%0A%09%09%09%09%09
@@ -2098,32 +2098,111 @@
    dst.close();%0A
+%09                %0A%09                System.out.println(backupDBPath.getPath());%0A
 %09               
@@ -2278,26 +2278,24 @@
 %09           
- %7D
 %0A%09        %7D%0A
@@ -2319,24 +2319,58 @@
 eption e) %7B%0A
+%09    %09%0A%09    %09e.printStackTrace();%0A
 %09    %7D%0A%09%09ret

Backup frament