public static void createSMS(Context context, String number, String text) {
   String name = Utils.getContactNameFromNumber(context, number);
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsSMSNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "message.bmp");
   String description = "SMS: " + name;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     if (Preferences.stickyNotifications & !number.equals("Google Chat")) {
       Bitmap[] bitmaps = smartNotify(context, icon, name, text);
       Notification.addBitmapNotification(context, bitmaps, vibratePattern, -1, description);
     } else {
       Bitmap bitmap = smartLines(context, icon, "SMS from", new String[] {name});
       Notification.addBitmapNotification(context, bitmap, vibratePattern, 4000, description);
       Notification.addTextNotification(
           context,
           text,
           Notification.VibratePattern.NO_VIBRATE,
           Notification.getDefaultNotificationTimeout(context));
     }
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, text, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "SMS from"),
         Protocol.createOled2lines(context, name, text),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createMMS(Context context, String number) {
   String name = Utils.getContactNameFromNumber(context, number);
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsSMSNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "message.bmp");
   String description = "MMS: " + name;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "MMS from", new String[] {name});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, name, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "MMS from"),
         Protocol.createOled2lines(context, name, ""),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createNewVoicemail(Context context) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsVoicemailNumberBuzzes");
   String description = "New Voicemail";
   Bitmap icon = Utils.getBitmap(context, "voicemail.bmp");
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap =
         smartLines(context, icon, "Voicemail", new String[] {"You have", "new voicemail"});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "Voicemail"),
         Protocol.createOled2lines(context, "You have", "new voicemail"),
         null,
         0,
         vibratePattern,
         description);
   }
 }
 public static void createBatterylow(Context context) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsBatteryNumberBuzzes");
   StringBuilder builder = new StringBuilder();
   builder.append(Monitors.BatteryData.level);
   builder.append("%");
   String description = "Battery low";
   Bitmap icon = Utils.getBitmap(context, "batterylow.bmp");
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap =
         smartLines(
             context, icon, "Battery", new String[] {"Phone battery at", builder.toString()});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "Warning!"),
         Protocol.createOled2lines(context, "Phone battery at", builder.toString()),
         null,
         0,
         vibratePattern,
         description);
   }
 }
 public static void createNMA(
     Context context, String appName, String event, String desc, int prio, String url) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsNMANumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "notifymyandroid.bmp");
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, appName, new String[] {event, desc});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         appName + ": " + event);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, desc, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, appName),
         Protocol.createOled2lines(context, event, desc),
         scroll,
         len,
         vibratePattern,
         appName + ": " + event);
   }
 }
 public static void createOtherNotification(
     Context context, Bitmap icon, String appName, String notificationText, int buzzes) {
   VibratePattern vibratePattern;
   if (buzzes != -1) {
     vibratePattern = createVibratePatternFromBuzzes(buzzes);
   } else {
     vibratePattern =
         createVibratePatternFromPreference(context, "settingsOtherNotificationNumberBuzzes");
   }
   if (icon == null) {
     icon = Utils.getBitmap(context, "notify.bmp");
   }
   String description = appName;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, appName, new String[] {notificationText});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, notificationText, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, appName),
         Protocol.createOled2lines(context, "Notification", notificationText),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createWinamp(Context context, String artist, String track, String album) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsMusicNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "winamp.bmp");
   String description = "Winamp: " + track;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "Winamp", new String[] {track, album, artist});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, track, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, artist),
         Protocol.createOled2lines(context, album, track),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createAlarm(Context context) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsAlarmNumberBuzzes");
   final Calendar t = Calendar.getInstance();
   final String currentTime = DateFormat.getTimeFormat(context).format(t.getTime());
   Bitmap icon = Utils.getBitmap(context, "timer.bmp");
   String description = "Alarm";
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap =
         smartLines(context, icon, "Alarm", new String[] {currentTime}, FontCache.FontSize.LARGE);
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "Alarm"),
         Protocol.createOled1line(context, null, currentTime),
         null,
         0,
         vibratePattern,
         description);
   }
 }
 public static void createTimezonechange(Context context) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsTimezoneNumberBuzzes");
   TimeZone tz = TimeZone.getDefault();
   Bitmap icon = Utils.getBitmap(context, "timezone.bmp");
   String description = "Timezone Changed";
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap =
         smartLines(
             context, icon, "Timezone", new String[] {"Timezone Changed", tz.getDisplayName()});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "Timezone"),
         Protocol.createOled1line(context, null, tz.getDisplayName()),
         null,
         0,
         vibratePattern,
         description);
   }
 }
 public static void createTouchdownMail(Context context, String title, String ticker) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsTDNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "email.bmp");
   String description = "TouchDown: " + title;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "TouchDown", new String[] {title, ticker});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, ticker, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "TouchDown"),
         Protocol.createOled2lines(context, title, ticker),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createCalendar(Context context, String text) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsCalendarNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "calendar.bmp");
   String description = "Cal: " + text;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "Calendar", new String[] {text});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, text, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "  Calendar"),
         Protocol.createOled2lines(context, "Event Reminder:", text),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createGmailBlank(Context context, String recipient, int count) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsGmailNumberBuzzes");
   String messages = count + " new " + (count == 1 ? "message" : "messages");
   Bitmap icon = Utils.getBitmap(context, "gmail.bmp");
   String description = "Gmail: unread " + count;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "Gmail", new String[] {messages, recipient});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, recipient, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, " Gmail"),
         Protocol.createOled2lines(context, messages, recipient),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createGmail(
     Context context, String sender, String email, String subject, String snippet) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsGmailNumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "gmail.bmp");
   String description = "Gmail: " + sender;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "Gmail", new String[] {sender, email, subject});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
     Notification.addTextNotification(
         context,
         snippet,
         Notification.VibratePattern.NO_VIBRATE,
         Notification.getDefaultNotificationTimeout(context));
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, snippet, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled2lines(context, "Gmail from " + sender, email),
         Protocol.createOled2lines(context, subject, snippet),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createK9(Context context, String sender, String subject, String folder) {
   VibratePattern vibratePattern =
       createVibratePatternFromPreference(context, "settingsK9NumberBuzzes");
   Bitmap icon = Utils.getBitmap(context, "email.bmp");
   String description = "K9: " + sender;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap bitmap = smartLines(context, icon, "K9 mail", new String[] {sender, subject, folder});
     Notification.addBitmapNotification(
         context,
         bitmap,
         vibratePattern,
         Notification.getDefaultNotificationTimeout(context),
         description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, subject, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, "K9 mail"),
         Protocol.createOled2lines(context, sender, subject),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
 public static void createSmart(
     Context context,
     String title,
     String text,
     Bitmap icon,
     boolean sticky,
     VibratePattern vibratePattern) {
   if (icon == null) {
     icon = Utils.getBitmap(context, "notify.bmp");
   }
   String description = "Smart: " + text;
   if (MetaWatchService.watchType == WatchType.DIGITAL) {
     Bitmap[] bitmaps;
     if (sticky) {
       bitmaps = smartNotify(context, icon, title, text);
     } else {
       bitmaps = new Bitmap[] {smartLines(context, icon, title, new String[] {text})};
     }
     Notification.addBitmapNotification(context, bitmaps, vibratePattern, -1, description);
   } else {
     byte[] scroll = new byte[800];
     int len = Protocol.createOled2linesLong(context, text, scroll);
     Notification.addOledNotification(
         context,
         Protocol.createOled1line(context, icon, title),
         Protocol.createOled2lines(context, title, text),
         scroll,
         len,
         vibratePattern,
         description);
   }
 }
  void readFromDevice() {

    if (MetaWatchService.fakeWatch) {
      try {
        Thread.sleep(10000);
      } catch (InterruptedException e) {
      }
      return;
    }

    try {
      byte[] bytes = new byte[256];
      if (Preferences.logging) Log.d(MetaWatch.TAG, "before blocking read");
      // Do a proper read loop
      int haveread = 0;
      int lengthtoread = 4;
      while ((lengthtoread - haveread) != 0) {
        haveread += inputStream.read(bytes, haveread, lengthtoread - haveread);
        if (haveread > 1) {
          lengthtoread = bytes[1];
        }
      }

      wakeLock.acquire(5000);

      // print received
      String str = "received: ";
      int len = (bytes[1] & 0xFF);
      if (Preferences.logging) Log.d(MetaWatch.TAG, "packet length: " + len);

      for (int i = 0; i < len; i++) {
        // str+= Byte.toString(bytes[i]) + ", ";
        str += "0x" + Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1) + ", ";
      }
      if (Preferences.logging) Log.d(MetaWatch.TAG, str);
      /*
       * switch (bytes[2]) { case eMessageType.GetDeviceTypeResponse.msg:
       * if (Preferences.logging) Log.d(MetaWatch.TAG, "received: device type response"); break;
       * case eMessageType.NvalOperationResponseMsg.msg:
       * if (Preferences.logging) Log.d(MetaWatch.TAG, "received: nval response"); break; case
       * eMessageType.StatusChangeEvent.msg: if (Preferences.logging) Log.d(MetaWatch.TAG,
       * "received: status change event"); break; }
       */
      /*
       * if (bytes[2] == 0x31) { // nval response if (bytes[3] == 0x00) //
       * success if (bytes[4] == 0x00) // set to 12 hour format
       * Protocol.setNvalTime(true); }
       */
      if (bytes[2] == eMessageType.NvalOperationResponseMsg.msg) {
        if (Preferences.logging)
          Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): NvalOperationResponseMsg");
        // Do something here?
      } else if (bytes[2] == eMessageType.StatusChangeEvent.msg) { // status
        // change
        // event
        if (Preferences.logging)
          Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): status change");
        if (bytes[4] == 0x01) {
          if (Preferences.logging)
            Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): mode changed");
          synchronized (Notification.modeChanged) {
            Notification.modeChanged.notify();
          }
        } else if (bytes[4] == 0x11) {
          if (Preferences.logging)
            Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): scroll request notification");

          synchronized (Notification.scrollRequest) {
            Notification.scrollRequest.notify();
          }
        } else if (bytes[4] == 0x10) {
          if (Preferences.logging)
            Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): scroll complete.");
        }
      } else if (bytes[2] == eMessageType.ButtonEventMsg.msg) { // button
        // press
        if (Preferences.logging)
          Log.d(MetaWatch.TAG, "MetaWatchService.readFromDevice(): button event");
        pressedButton(bytes[3] & 0xFF); //
      } else if (bytes[2] == eMessageType.GetDeviceTypeResponse.msg) { // device
        // type
        if (bytes[4] == 1 || bytes[4] == 4) {
          watchType = WatchType.ANALOG;
          if (Preferences.logging)
            Log.d(
                MetaWatch.TAG,
                "MetaWatchService.readFromDevice(): device type response; analog watch");

          if (watchState == WatchStates.OFF || watchState == WatchStates.IDLE) {
            Idle.toIdle(this);
            Idle.updateIdle(this, true);
          }

          SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
          boolean displaySplash = sharedPreferences.getBoolean("DisplaySplashScreen", true);
          if (displaySplash) {
            Protocol.sendOledBitmap(
                Utils.getBitmap(this, "splash_16_0.bmp"),
                MetaWatchService.WatchBuffers.NOTIFICATION,
                0);
            Protocol.sendOledBitmap(
                Utils.getBitmap(this, "splash_16_1.bmp"),
                MetaWatchService.WatchBuffers.NOTIFICATION,
                1);
          }

        } else {
          watchType = WatchType.DIGITAL;
          if (Preferences.logging)
            Log.d(
                MetaWatch.TAG,
                "MetaWatchService.readFromDevice(): device type response; digital watch");

          Protocol.configureMode();

          if (watchState == WatchStates.OFF || watchState == WatchStates.IDLE) {
            Idle.toIdle(this);
            Idle.updateIdle(this, true);
          }

          SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
          boolean displaySplash = sharedPreferences.getBoolean("DisplaySplashScreen", true);
          if (displaySplash) {
            Notification.addBitmapNotification(
                this,
                Utils.getBitmap(this, "splash.png"),
                new VibratePattern(false, 0, 0, 0),
                10000,
                "Splash");
          }

          Protocol.queryNvalTime();
        }

        SharedPreferences sharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(context);

        /* Notify watch on connection if requested. */
        boolean notifyOnConnect = sharedPreferences.getBoolean("NotifyWatchOnConnect", false);
        if (Preferences.logging)
          Log.d(MetaWatch.TAG, "MetaWatchService.connect(): notifyOnConnect=" + notifyOnConnect);
        if (notifyOnConnect) {
          NotificationBuilder.createOtherNotification(
              context,
              null,
              "MetaWatch",
              getResources().getString(R.string.connection_connected),
              1);
        }

        Idle.activateButtons(this);

      } else if (bytes[2] == eMessageType.ReadBatteryVoltageResponse.msg) {
        boolean powerGood = bytes[4] > 0;
        boolean batteryCharging = bytes[5] > 0;
        float batterySense = (((int) bytes[7] << 8) + (int) bytes[6]) / 1000.0f;
        float batteryAverage = (((int) bytes[9] << 8) + (int) bytes[8]) / 1000.0f;
        if (Preferences.logging)
          Log.d(
              MetaWatch.TAG,
              "MetaWatchService.readFromDevice(): received battery voltage response."
                  + " power_good="
                  + powerGood
                  + " battery_charging="
                  + batteryCharging
                  + " battery_sense="
                  + batterySense
                  + " battery_average="
                  + batteryAverage);
        String voltageFrequencyString =
            PreferenceManager.getDefaultSharedPreferences(this)
                .getString("collectWatchVoltage", "0");
        final int voltageFrequency = Integer.parseInt(voltageFrequencyString);
        if (voltageFrequency > 0) {
          File sdcard = Environment.getExternalStorageDirectory();
          File csv = new File(sdcard, "metawatch_voltage.csv");
          boolean fileExists = csv.exists();
          FileWriter fw = new FileWriter(csv, true);
          if (fileExists == false) {
            fw.write("Date,Sense,Average\n");
          }
          Date date = new Date();
          fw.write("\"" + date.toString() + "\"," + batterySense + "," + batteryAverage + "\n");
          fw.flush();
          fw.close();
        }
      } else if (bytes[2] == eMessageType.ReadLightSensorResponse.msg) {
        float lightSense = (((int) bytes[1] << 8) + (int) bytes[0]) / 1000.0f;
        float lightAverage = (((int) bytes[3] << 8) + (int) bytes[2]) / 1000.0f;
        if (Preferences.logging)
          Log.d(
              MetaWatch.TAG,
              "MetaWatchService.readFromDevice(): received light sensor response."
                  + " light_sense="
                  + lightSense
                  + " light_average="
                  + lightAverage);
      } else if (bytes[2] == eMessageType.GetRealTimeClockResponse.msg) {
        long timeNow = System.currentTimeMillis();
        long roundTrip = timeNow - Monitors.getRTCTimestamp;

        if (Preferences.logging)
          Log.d(
              MetaWatch.TAG,
              "MetaWatchService.readFromDevice(): received rtc response."
                  + " round trip= "
                  + roundTrip);

        Monitors.rtcOffset = (int) (roundTrip / 2000);

        Protocol.sendRtcNow(context);

      } else {
        if (Preferences.logging)
          Log.d(
              MetaWatch.TAG,
              "MetaWatchService.readFromDevice(): Unknown message : 0x"
                  + Integer.toString((bytes[2] & 0xff) + 0x100, 16).substring(1)
                  + ", ");
      }

    } catch (IOException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
      resetConnection();
    } catch (ArrayIndexOutOfBoundsException e) {
      if (Preferences.logging) Log.d(MetaWatch.TAG, e.toString());
      resetConnection();
    } finally {
      if (wakeLock != null && wakeLock.isHeld()) {
        wakeLock.release();
      }
    }
  }