// Path: wear/src/main/java/ooo/oxo/apps/earth/dao/Earth.java
// public class Earth {
// 
//     public String file;
// 
//     public Date fetchedAt = new Date();
// 
//     public Earth() {
//     }
// 
//     public Earth(String file) {
//         this.file = file;
//     }
// 
//     @Nullable
//     public static Earth fromCursor(@NonNull Cursor cursor) {
//         if (!cursor.moveToNext()) {
//             return null;
//         }
// 
//         Earth earth = new Earth();
//         earth.file = cursor.getString(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FILE));
//         earth.fetchedAt = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FETCHED_AT)));
// 
//         if (TextUtils.isEmpty(earth.file)) {
//             return null;
//         }
// 
//         return earth;
//     }
// 
//     public ContentValues toContentValues() {
//         ContentValues values = new ContentValues();
//         values.put(EarthsContract.Columns.FILE, file);
//         values.put(EarthsContract.Columns.FETCHED_AT, fetchedAt.getTime());
//         return values;
//     }
// 
// }
// 
// Path: wear/src/main/java/ooo/oxo/apps/earth/provider/EarthsContract.java
// public class EarthsContract {
// 
//     public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".earths";
// 
//     public static final Uri CONTENT_URI = new Uri.Builder()
//             .scheme(ContentResolver.SCHEME_CONTENT)
//             .authority(AUTHORITY)
//             .path("earths")
//             .build();
// 
//     public static final Uri LATEST_CONTENT_URI = Uri.withAppendedPath(CONTENT_URI, "latest");
// 
//     public static final String CONTENT_TYPE_DIR
//             = "vnd.android.cursor.dir/vnd." + BuildConfig.APPLICATION_ID + ".earths";
// 
//     public static final String CONTENT_TYPE_ITEM
//             = "vnd.android.cursor.item/vnd." + BuildConfig.APPLICATION_ID + ".earths";
// 
//     static final String TABLE = "earths";
// 
//     public static class Columns implements BaseColumns {
//         public static final String FILE = "file";
//         public static final String FETCHED_AT = "fetched_at";
//     }
// 
// }


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import ooo.oxo.apps.earth.dao.Earth;
import ooo.oxo.apps.earth.provider.EarthsContract;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItemAsset;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;
import org.apache.commons.io.FileUtils;
                .build();

        ConnectionResult connection = client.blockingConnect(10, TimeUnit.SECONDS);

        if (!connection.isSuccess()) {
            Log.e(TAG, "failed to connect to GoogleApiClient: " + connection.getErrorCode());
            return;
        }

        InputStream input = Wearable.DataApi.getFdForAsset(client, asset)
                .await()
                .getInputStream();

        client.disconnect();

        if (input == null) {
            Log.e(TAG, "failed to get asset");
            return;
        }

        File file = new File(getCacheDir(), String.valueOf("earth_" + System.currentTimeMillis()));

        try {
            FileUtils.copyInputStreamToFile(input, file);
        } catch (IOException e) {
            Log.e(TAG, "failed to save asset", e);
            return;
        }

        Earth earth = new Earth(file.getAbsolutePath());
// Path: wear/src/main/java/ooo/oxo/apps/earth/dao/Earth.java
// public class Earth {
// 
//     public String file;
// 
//     public Date fetchedAt = new Date();
// 
//     public Earth() {
//     }
// 
//     public Earth(String file) {
//         this.file = file;
//     }
// 
//     @Nullable
//     public static Earth fromCursor(@NonNull Cursor cursor) {
//         if (!cursor.moveToNext()) {
//             return null;
//         }
// 
//         Earth earth = new Earth();
//         earth.file = cursor.getString(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FILE));
//         earth.fetchedAt = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FETCHED_AT)));
// 
//         if (TextUtils.isEmpty(earth.file)) {
//             return null;
//         }
// 
//         return earth;
//     }
// 
//     public ContentValues toContentValues() {
//         ContentValues values = new ContentValues();
//         values.put(EarthsContract.Columns.FILE, file);
//         values.put(EarthsContract.Columns.FETCHED_AT, fetchedAt.getTime());
//         return values;
//     }
// 
// }
// 
// Path: wear/src/main/java/ooo/oxo/apps/earth/provider/EarthsContract.java
// public class EarthsContract {
// 
//     public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".earths";
// 
//     public static final Uri CONTENT_URI = new Uri.Builder()
//             .scheme(ContentResolver.SCHEME_CONTENT)
//             .authority(AUTHORITY)
//             .path("earths")
//             .build();
// 
//     public static final Uri LATEST_CONTENT_URI = Uri.withAppendedPath(CONTENT_URI, "latest");
// 
//     public static final String CONTENT_TYPE_DIR
//             = "vnd.android.cursor.dir/vnd." + BuildConfig.APPLICATION_ID + ".earths";
// 
//     public static final String CONTENT_TYPE_ITEM
//             = "vnd.android.cursor.item/vnd." + BuildConfig.APPLICATION_ID + ".earths";
// 
//     static final String TABLE = "earths";
// 
//     public static class Columns implements BaseColumns {
//         public static final String FILE = "file";
//         public static final String FETCHED_AT = "fetched_at";
//     }
// 
// }


// Path: wear/src/main/java/ooo/oxo/apps/earth/WatchSyncService.java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import ooo.oxo.apps.earth.dao.Earth;
import ooo.oxo.apps.earth.provider.EarthsContract;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItemAsset;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;
import org.apache.commons.io.FileUtils;

                .build();

        ConnectionResult connection = client.blockingConnect(10, TimeUnit.SECONDS);

        if (!connection.isSuccess()) {
            Log.e(TAG, "failed to connect to GoogleApiClient: " + connection.getErrorCode());
            return;
        }

        InputStream input = Wearable.DataApi.getFdForAsset(client, asset)
                .await()
                .getInputStream();

        client.disconnect();

        if (input == null) {
            Log.e(TAG, "failed to get asset");
            return;
        }

        File file = new File(getCacheDir(), String.valueOf("earth_" + System.currentTimeMillis()));

        try {
            FileUtils.copyInputStreamToFile(input, file);
        } catch (IOException e) {
            Log.e(TAG, "failed to save asset", e);
            return;
        }

        Earth earth = new Earth(file.getAbsolutePath());
        getContentResolver().insert(EarthsContract.CONTENT_URI, earth.toContentValues());