Skip to content

Commit

Permalink
Merge pull request #7 from dabbler011/master
Browse files Browse the repository at this point in the history
backup and retrieval added
  • Loading branch information
opticod authored Jan 30, 2018
2 parents 9b92da2 + 80972e8 commit dc5bb68
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ dependencies {
debugCompile 'com.facebook.stetho:stetho:1.3.1'
// Firebase Authentication
compile 'com.google.firebase:firebase-auth:9.8.0'
compile 'com.google.firebase:firebase-database:9.8.0'
// Google Sign In SDK (only required for Google Sign In)
compile 'com.google.android.gms:play-services-auth:9.8.0'

compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
apply plugin: 'com.google.gms.google-services'
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import work.technie.motonavigator.fragment.DriveCollectionFragment;
import work.technie.motonavigator.fragment.DriveFragment;
import work.technie.motonavigator.fragment.MapFragment;
import work.technie.motonavigator.utils.Utility;

/**
* Created by anupam on 31/10/16.
Expand All @@ -63,15 +64,18 @@ public class BaseActivity extends AppCompatActivity
private final String FRAGMENT_TAG_REST = "FTAGR";
private int currentMenuItemId;

private FirebaseAuth mAuth;
private FirebaseUser user;

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

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();

View hView = navigationView.getHeaderView(0);
TextView nav_user_name = (TextView) hView.findViewById(R.id.user_name);
Expand Down Expand Up @@ -163,6 +167,10 @@ private void doMenuAction(int menuItemId) {
case R.id.nav_sign_out:
signOut();
break;
case R.id.nav_backup:
if(user!=null) {
Utility.backUpData(getApplicationContext(), user.getUid());
}
default:
//nothing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
* limitations under the License.
*/
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
Expand All @@ -57,8 +59,14 @@
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import work.technie.motonavigator.R;
import work.technie.motonavigator.data.MotorContract;

/**
* Created by anupam on 29/10/16.
Expand Down Expand Up @@ -191,6 +199,71 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
editor.putInt(getString(R.string.login_mode), GOOGLE_SIGN_IN);
editor.apply();


final Uri waypointUri = MotorContract.Waypoints.buildWaypointUri();
this.getContentResolver().delete(waypointUri,null,null);

final Uri stepsUri = MotorContract.Steps.buildStepUri();
this.getContentResolver().delete(stepsUri,null,null);

if(mAuth.getCurrentUser() != null) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference waypointsRef = database.getReference(mAuth.getCurrentUser().getUid())
.child("STEPS");
waypointsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
ContentValues contentValue = new ContentValues();
contentValue.put(MotorContract.Steps.ROUTE_ID, messageSnapshot.child(MotorContract.Steps.ROUTE_ID).getValue().toString());
contentValue.put(MotorContract.Steps.BEARING_BEFORE, messageSnapshot.child(MotorContract.Steps.BEARING_BEFORE).getValue().toString());
contentValue.put(MotorContract.Steps.BEARING_AFTER, messageSnapshot.child(MotorContract.Steps.BEARING_AFTER).getValue().toString());
contentValue.put(MotorContract.Steps.LOCATION_LAT, messageSnapshot.child(MotorContract.Steps.LOCATION_LAT).getValue().toString());
contentValue.put(MotorContract.Steps.LOCATION_LONG, messageSnapshot.child(MotorContract.Steps.LOCATION_LONG).getValue().toString());
contentValue.put(MotorContract.Steps.TYPE, messageSnapshot.child(MotorContract.Steps.TYPE).getValue().toString());
contentValue.put(MotorContract.Steps.INSTRUCTION, messageSnapshot.child(MotorContract.Steps.INSTRUCTION).getValue().toString());
contentValue.put(MotorContract.Steps.MODE, messageSnapshot.child(MotorContract.Steps.MODE).getValue().toString());
contentValue.put(MotorContract.Steps.DURATION, messageSnapshot.child(MotorContract.Steps.DURATION).getValue().toString());
contentValue.put(MotorContract.Steps.NAME, messageSnapshot.child(MotorContract.Steps.NAME).getValue().toString());
contentValue.put(MotorContract.Steps.DISTANCE, messageSnapshot.child(MotorContract.Steps.DISTANCE).getValue().toString());

getApplicationContext().getContentResolver().insert(stepsUri, contentValue);
}
}

@Override
public void onCancelled(DatabaseError firebaseError) {
}
});

DatabaseReference stepRef = database.getReference(mAuth.getCurrentUser().getUid())
.child("WAYPOINTS");
stepRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
ContentValues contentValue = new ContentValues();
contentValue.put(MotorContract.Waypoints.ROUTE_ID, messageSnapshot.child(MotorContract.Waypoints.ROUTE_ID).getValue().toString());
contentValue.put(MotorContract.Waypoints.START_NAME, messageSnapshot.child(MotorContract.Waypoints.START_NAME).getValue().toString());
contentValue.put(MotorContract.Waypoints.START_LAT, messageSnapshot.child(MotorContract.Waypoints.START_LAT).getValue().toString());
contentValue.put(MotorContract.Waypoints.START_LONG, messageSnapshot.child(MotorContract.Waypoints.START_LONG).getValue().toString());
contentValue.put(MotorContract.Waypoints.DEST_NAME, messageSnapshot.child(MotorContract.Waypoints.DEST_NAME).getValue().toString());
contentValue.put(MotorContract.Waypoints.DEST_LAT, messageSnapshot.child(MotorContract.Waypoints.DEST_LAT).getValue().toString());
contentValue.put(MotorContract.Waypoints.DEST_LONG, messageSnapshot.child(MotorContract.Waypoints.DEST_LONG).getValue().toString());
contentValue.put(MotorContract.Waypoints.MODE, messageSnapshot.child(MotorContract.Waypoints.MODE).getValue().toString());
contentValue.put(MotorContract.Waypoints.ROUTE_DURATION, messageSnapshot.child(MotorContract.Waypoints.ROUTE_DURATION).getValue().toString());
contentValue.put(MotorContract.Waypoints.ROUTE_DISTANCE, messageSnapshot.child(MotorContract.Waypoints.ROUTE_DISTANCE).getValue().toString());

getApplicationContext().getContentResolver().insert(waypointUri, contentValue);
}
}

@Override
public void onCancelled(DatabaseError firebaseError) {
}
});
}

Intent intent = new Intent(this, work.technie.motonavigator.activity.BaseActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
Expand Down
119 changes: 119 additions & 0 deletions app/src/main/java/work/technie/motonavigator/utils/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import work.technie.motonavigator.data.MotorContract;


/**
Expand All @@ -27,6 +47,37 @@ public class Utility {
private static final String WIFI = "WIFI";
private static final String MOBILE = "MOBILE";

private static final String[] WAYPOINTS_COLUMNS = {

MotorContract.Waypoints.TABLE_NAME + "." + MotorContract.Waypoints._ID,
MotorContract.Waypoints.START_NAME,
MotorContract.Waypoints.START_LAT,
MotorContract.Waypoints.START_LONG,
MotorContract.Waypoints.DEST_NAME,
MotorContract.Waypoints.DEST_LAT,
MotorContract.Waypoints.DEST_LONG,
MotorContract.Waypoints.MODE,
MotorContract.Waypoints.ROUTE_ID,
MotorContract.Waypoints.ROUTE_DURATION,
MotorContract.Waypoints.ROUTE_DISTANCE
};

private static final String[] STEPS_COLUMNS = {

MotorContract.Steps.TABLE_NAME + "." + MotorContract.Waypoints._ID,
MotorContract.Steps.ROUTE_ID,
MotorContract.Steps.BEARING_BEFORE,
MotorContract.Steps.BEARING_AFTER,
MotorContract.Steps.LOCATION_LAT,
MotorContract.Steps.LOCATION_LONG,
MotorContract.Steps.TYPE,
MotorContract.Steps.INSTRUCTION,
MotorContract.Steps.MODE,
MotorContract.Steps.DURATION,
MotorContract.Steps.NAME,
MotorContract.Steps.DISTANCE
};

public static boolean hasNetworkConnection(Context context) {
boolean hasConnectedWifi = false;
boolean hasConnectedMobile = false;
Expand All @@ -43,4 +94,72 @@ public static boolean hasNetworkConnection(Context context) {
}
return hasConnectedWifi || hasConnectedMobile;
}

public static void backUpData(Context mContext,String uid) {
Uri waypointUri = MotorContract.Waypoints.buildWaypointUri();
Cursor waypointCursor = mContext.getContentResolver().query(waypointUri, WAYPOINTS_COLUMNS, null, null, null);

try {
putToFirebase(waypointCursor,uid,"WAYPOINTS");
} catch (JSONException e) {
e.printStackTrace();
}

Uri stepsUri = MotorContract.Steps.buildStepUri();
Cursor stepsCursor = mContext.getContentResolver().query(stepsUri, STEPS_COLUMNS, null, null, null);

try {
putToFirebase(stepsCursor,uid,"STEPS");
} catch (JSONException e) {
e.printStackTrace();
}
}

private static void putToFirebase(Cursor cursor,String uid,String tableName) throws JSONException {

int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
do {
JSONObject row = new JSONObject();
for (int index = 1; index < columnCount; index++) {
row.put(cursor.getColumnName(index), cursor.getString(index));
}
new OkHttpHandler().execute(row.toString(),uid,tableName,cursor.getString(0));
} while (cursor.moveToNext());

cursor.close();
}

private static class OkHttpHandler extends AsyncTask<String, Void, byte[]> {

private final OkHttpClient client = new OkHttpClient();
private final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected byte[] doInBackground(String... params) {

RequestBody body = RequestBody.create(JSON, params[0]);
Request request = new Request.Builder()
.url("https://moto-navigator-74a33.firebaseio.com/" + params[1] + "/" + params[2]+ "/" + params[3] + ".json")
.put(body)
.build();
try {
Response response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(byte[] bytes) {
super.onPostExecute(bytes);
}
}
}
Binary file added app/src/main/res/drawable-v21/ic_backup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
android:id="@+id/nav_directions"
android:icon="@drawable/ic_navigation_black_36dp"
android:title="@string/drive" />

<item
android:id="@+id/nav_backup"
android:icon="@drawable/ic_backup"
android:title="@string/backup" />
</group>

<group
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@
<string name="sign_out">Sign Out</string>
<string name="start_navigation">Start Navigation</string>
<string name="toogle_location_service">Toogle Location Service</string>
<string name="backup">Backup Data</string>

</resources>

0 comments on commit dc5bb68

Please sign in to comment.