com.google.android.gms.nearby.messages.Message Java Examples

The following examples show how to use com.google.android.gms.nearby.messages.Message. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RoomData.java    From justaline-android with Apache License 2.0 6 votes vote down vote up
public RoomData(Message message) {
    this.message = message;
    String messageString = new String(message.getContent());
    String[] parts = messageString.split(",");
    if (parts.length == 2) {
        try {
            key = parts[0];
            timestamp = Long.parseLong(parts[1]);
        } catch (RuntimeException e) {
            throw new MalformedDataException(
                    "Message does not meet format <int:code>,<long:timestamp>: "
                            + messageString);
        }
    } else {
        throw new MalformedDataException(
                "Message does not meet format <code>,<timestamp>: " + messageString);
    }
}
 
Example #2
Source File: BaseNearbyActivity.java    From friendspell with Apache License 2.0 5 votes vote down vote up
@Override
public void onFound(final Message message) {
  String json = new String(message.getContent());
  final NearbyPerson person = GSON.fromJson(json, NearbyPerson.class);
  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      addPerson(person);
      updateList();
    }
  });
}
 
Example #3
Source File: Utils.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Saves a message string to {@link SharedPreferences}.
 *
 * @param context The context.
 * @param message The Message whose payload (as string) is saved to SharedPreferences.
 */
static void saveFoundMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    Set<String> cachedMessagesSet = new HashSet<>(cachedMessages);
    String messageString = new String(message.getContent());
    if (!cachedMessagesSet.contains(messageString)) {
        cachedMessages.add(0, new String(message.getContent()));
        getSharedPreferences(context)
                .edit()
                .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
                .apply();
    }
}
 
Example #4
Source File: Utils.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a message string from {@link SharedPreferences}.
 * @param context The context.
 * @param message The Message whose payload (as string) is removed from SharedPreferences.
 */
static void removeLostMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    cachedMessages.remove(new String(message.getContent()));
    getSharedPreferences(context)
            .edit()
            .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
            .apply();
}
 
Example #5
Source File: Utils.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a message string from {@link SharedPreferences}.
 * @param context The context.
 * @param message The Message whose payload (as string) is removed from SharedPreferences.
 */
static void removeLostMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    cachedMessages.remove(new String(message.getContent()));
    getSharedPreferences(context)
            .edit()
            .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
            .apply();
}
 
Example #6
Source File: NearbyMessageIntentService.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onFound(Message message) {
    String url = PhysicalWebBleClient.getInstance().getUrlFromMessage(message);
    if (url != null) {
        UrlManager.getInstance().addUrl(new UrlInfo(url));
    }
}
 
Example #7
Source File: NearbyMessageIntentService.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onLost(Message message) {
    String url = PhysicalWebBleClient.getInstance().getUrlFromMessage(message);
    if (url != null) {
        UrlManager.getInstance().removeUrl(new UrlInfo(url));
    }
}
 
Example #8
Source File: DeviceMessage.java    From io16experiment-master with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@code DeviceMessage} object from the string used to construct the payload to a
 * {@code Nearby} {@code Message}.
 */
public static DeviceMessage fromNearbyMessage(Message message) {
    String nearbyMessageString = new String(message.getContent()).trim();
    return gson.fromJson(
            (new String(nearbyMessageString.getBytes(Charset.forName("UTF-8")))),
            DeviceMessage.class);
}
 
Example #9
Source File: GoogleApiClientBridge.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public Message publish(String token, byte[] data, ResultCallback<Status> callback) {
  Message message = new Message(data);
  GoogleApiClient googleApiClient = clients.get(token);
  if (googleApiClient.isConnected()) {
    Nearby.Messages.publish(googleApiClient, message).setResultCallback(callback);
  }
  return message;
}
 
Example #10
Source File: GoogleApiClientBridge.java    From friendspell with Apache License 2.0 5 votes vote down vote up
public void unpublish(String token, Message message, ResultCallback<Status> callback) {
  GoogleApiClient googleApiClient = clients.get(token);
  if (googleApiClient.isConnected()) {
    Nearby.Messages.unpublish(googleApiClient, message)
        .setResultCallback(callback);
  }
}
 
Example #11
Source File: Utils.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Saves a message string to {@link SharedPreferences}.
 *
 * @param context The context.
 * @param message The Message whose payload (as string) is saved to SharedPreferences.
 */
static void saveFoundMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    Set<String> cachedMessagesSet = new HashSet<>(cachedMessages);
    String messageString = new String(message.getContent());
    if (!cachedMessagesSet.contains(messageString)) {
        cachedMessages.add(0, new String(message.getContent()));
        getSharedPreferences(context)
                .edit()
                .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
                .apply();
    }
}
 
Example #12
Source File: NearbyForegroundSubscription.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onDistanceChanged(Message message, final Distance distance) {
    final String url = PhysicalWebBleClient.getInstance().getUrlFromMessage(message);
    if (url == null) return;

    ThreadUtils.postOnUiThread(new Runnable() {
        @Override
        public void run() {
            UrlManager.getInstance().addUrl(
                    new UrlInfo(url).setDistance(distance.getMeters()));
        }
    });
}
 
Example #13
Source File: PhysicalWebBleClient.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onFound(Message message) {
    final String url = PhysicalWebBleClient.getInstance().getUrlFromMessage(message);
    if (url != null) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                UrlManager.getInstance().addUrl(new UrlInfo(url));
            }
        });
    }
}
 
Example #14
Source File: PhysicalWebBleClient.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onLost(Message message) {
    final String url = PhysicalWebBleClient.getInstance().getUrlFromMessage(message);
    if (url != null) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                UrlManager.getInstance().removeUrl(new UrlInfo(url));
            }
        });
    }
}
 
Example #15
Source File: MainActivity.java    From nearby-beacons with MIT License 5 votes vote down vote up
@Override
public void onFound(Message message) {
    Log.i(TAG, "Found message: " + message);
    final OfferBeacon beacon = new OfferBeacon(message);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mAdapter.add(beacon);
        }
    });
}
 
Example #16
Source File: MainActivity.java    From nearby-beacons with MIT License 5 votes vote down vote up
@Override
public void onLost(Message message) {
    Log.i(TAG, "Lost message: " + message);
    final OfferBeacon beacon = new OfferBeacon(message);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mAdapter.remove(beacon);
        }
    });
}
 
Example #17
Source File: BeaconService.java    From nearby-beacons with MIT License 5 votes vote down vote up
@Override
public void onFound(Message message) {
    Log.i(TAG, "Found Background Beacon: "
            + new String(message.getContent()));
    boolean added = mDetectedBeacons.add(new OfferBeacon(message));
    if (added) {
        postScanResultNotification(mDetectedBeacons.size());
    }
}
 
Example #18
Source File: OfferBeacon.java    From nearby-beacons with MIT License 5 votes vote down vote up
public OfferBeacon(Message message) {
    if (!TYPE.equals(message.getType())) {
        throw new IllegalArgumentException(
                "Incorrect beacon message type: " + message.getType());
    }

    String json = new String(message.getContent());
    try {
        JSONObject parsed = new JSONObject(json);
        section = parsed.getString("section");
        offer = parsed.getString("latest_offer");
    } catch (JSONException e) {
        throw new IllegalArgumentException("Invalid JSON Contents");
    }
}
 
Example #19
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateBeaconsAdapter(Message message){
    if (!checkIfnearbyDeviceAlreadyExists(new String(message.getContent(), Charset.forName("UTF-8")))) {
        Log.v(TAG, "Adding message");
        mNearbyDevicesMessageList.add(message);
        updateAdapter(false);
    }
}
 
Example #20
Source File: DeviceMessage.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@code DeviceMessage} object from the string used to construct the payload to a
 * {@code Nearby} {@code Message}.
 */
public static DeviceMessage fromNearbyMessage(Message message) {
    String nearbyMessageString = new String(message.getContent()).trim();
    return gson.fromJson(
            (new String(nearbyMessageString.getBytes(Charset.forName("UTF-8")))),
            DeviceMessage.class);
}
 
Example #21
Source File: Utils.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Saves a message string to {@link SharedPreferences}.
 *
 * @param context The context.
 * @param message The Message whose payload (as string) is saved to SharedPreferences.
 */
static void saveFoundMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    Set<String> cachedMessagesSet = new HashSet<>(cachedMessages);
    String messageString = new String(message.getContent());
    if (!cachedMessagesSet.contains(messageString)) {
        cachedMessages.add(0, new String(message.getContent()));
        getSharedPreferences(context)
                .edit()
                .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
                .apply();
    }
}
 
Example #22
Source File: Utils.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a message string from {@link SharedPreferences}.
 * @param context The context.
 * @param message The Message whose payload (as string) is removed from SharedPreferences.
 */
static void removeLostMessage(Context context, Message message) {
    ArrayList<String> cachedMessages = new ArrayList<>(getCachedMessages(context));
    cachedMessages.remove(new String(message.getContent()));
    getSharedPreferences(context)
            .edit()
            .putString(KEY_CACHED_MESSAGES, new Gson().toJson(cachedMessages))
            .apply();
}
 
Example #23
Source File: RoomData.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
public RoomData(String key, Long timestamp) {
    this.key = key;
    this.timestamp = timestamp;

    String messageString = key + "," + timestamp.toString();
    message = new Message(messageString.getBytes());
}
 
Example #24
Source File: NearbyBackgroundService.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void displayNotification(Message message){
    if (!checkIfnearbyDeviceAlreadyExists(new String(message.getContent(), Charset.forName("UTF-8")))) {
        Log.i(Utils.TAG, "Adding message");
        mNearbyDevicesMessageList.add(message);
        Log.i(Utils.TAG, "count after adding: " + mNearbyDevicesMessageList.size());
        createNotification();
    }
}
 
Example #25
Source File: NearbyBackgroundService.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateNotification(Message message){
    if (checkIfnearbyDeviceAlreadyExists(new String(message.getContent(), Charset.forName("UTF-8")))) {
        Log.i(Utils.TAG, "removing message: " + message);
        removeLostNearbyMessage(new String(message.getContent(), Charset.forName("UTF-8")));
        Log.i(Utils.TAG, "count after removing: " + mNearbyDevicesMessageList.size());
        createNotification();
    }
}
 
Example #26
Source File: NearbyBackgroundService.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ArrayList<Message> loadNearbyMessageListForNotification(){
    final ArrayList<Message> nearbyMessageList = new ArrayList<>();
    for(int i = 0; i < mNearbyDevicesMessageList.size(); i++){
        nearbyMessageList.add(mNearbyDevicesMessageList.get(i));
    }
    return  nearbyMessageList;
}
 
Example #27
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    final Message message = intent.getParcelableExtra(action);
    switch (action) {
        case NEW_MESSAGE_FOUND:
            updateBeaconsAdapter(message);
            break;
        case MESSAGE_LOST:
            removeLostNearbyMessage(new String(message.getContent()));
            break;
    }
}
 
Example #28
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void displayNotification(Message message){
    if (!checkIfnearbyDeviceAlreadyExists(new String(message.getContent(), Charset.forName("UTF-8")))) {
        Log.v(TAG, "Adding message");
        mNearbyDevicesMessageList.add(message);
        updateAdapter(false);
        Log.v(TAG, "count after adding: " + mNearbyDevicesMessageList.size());
        createNotification();
    }
}
 
Example #29
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createNotification() {

        if(mNearbyDevicesMessageList.size() == 0){
            mNotificationManager.cancelAll();
            return;
        }

        final ArrayList<Message> nearbyMessageList = loadNearbyMessageListForNotification();
        mParentIntent.putExtra(NEARBY_DEVICE_DATA, nearbyMessageList);
        mParentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mPendingIntent = PendingIntent.getActivities(getActivity(), OPEN_ACTIVITY_REQ, new Intent[]{mParentIntent}, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getActivity())
                        .setSmallIcon(R.drawable.ic_eddystone)
                        .setColor(ContextCompat.getColor(getActivity(), R.color.actionBarColor))
                        .setContentTitle(getString(R.string.app_name))
                        .setContentIntent(mPendingIntent);

        if(mNearbyDevicesMessageList.size() == 1) {
            mBuilder.setContentText(new String(mNearbyDevicesMessageList.get(0).getContent(), Charset.forName("UTF-8")));
        } else {
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(getString(R.string.app_name));
            inboxStyle.setSummaryText(mNearbyDevicesMessageList.size() + " beacons found");
            mBuilder.setContentText(mNearbyDevicesMessageList.size() + " beacons found");
            for (int i = 0; i < mNearbyDevicesMessageList.size(); i++) {
                inboxStyle.addLine(new String(mNearbyDevicesMessageList.get(i).getContent(), Charset.forName("UTF-8")));
            }
            mBuilder.setStyle(inboxStyle);
        }
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
 
Example #30
Source File: BeaconsFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateNotification(Message message){
    if (checkIfnearbyDeviceAlreadyExists(new String(message.getContent(), Charset.forName("UTF-8")))) {
        Log.v(TAG, "removing message: " + message);
        removeLostNearbyMessage(new String(message.getContent(), Charset.forName("UTF-8")));
        Log.v(TAG, "count after removing: " + mNearbyDevicesMessageList.size());
        createNotification();
    }
}