Guest User

Skype AdvancedSettings.java v04 - theos0o

a guest
Jul 21st, 2011
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.72 KB | None | 0 0
  1. package com.skype;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8. import android.app.AlertDialog;
  9. import android.app.Service;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.content.SharedPreferences;
  14. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  15. import android.graphics.Bitmap;
  16. import android.graphics.Bitmap.Config;
  17. import android.graphics.BitmapFactory;
  18. import android.graphics.Canvas;
  19. import android.graphics.Color;
  20. import android.graphics.ImageFormat;
  21. import android.graphics.Paint;
  22. import android.graphics.Rect;
  23. import android.graphics.YuvImage;
  24. import android.hardware.Camera;
  25. import android.hardware.Camera.CameraInfo;
  26. import android.hardware.Camera.Size;
  27. import android.media.AudioManager;
  28. import android.net.Uri;
  29. import android.os.Bundle;
  30. import android.preference.CheckBoxPreference;
  31. import android.preference.EditTextPreference;
  32. import android.preference.ListPreference;
  33. import android.preference.Preference;
  34. import android.preference.PreferenceActivity;
  35. import android.preference.PreferenceCategory;
  36. import android.preference.PreferenceScreen;
  37. import android.preference.Preference.OnPreferenceClickListener;
  38. import android.preference.RingtonePreference;
  39. import android.provider.Settings;
  40. import android.util.Log;
  41. import android.view.MenuItem;
  42. import android.widget.Toast;
  43.  
  44. public class AdvancedSettings extends PreferenceActivity implements
  45.         OnSharedPreferenceChangeListener {
  46.  
  47.     private static final String PREFS_NAME = "skypeCustomSettings";
  48.     private static final String buildNo = "v04";
  49.  
  50.     public static Context context = null;
  51.  
  52.     public static int lastCamID = -1;
  53.     public static boolean speakerPhoneOn = false;
  54.     public static boolean bluetoothAudioOn = false;
  55.    
  56.     public static AudioManager am = null;
  57.  
  58.     SharedPreferences settings = null;
  59.     SharedPreferences.Editor editor = null;
  60.  
  61.     int numCams = -1;
  62.  
  63.     @Override
  64.     protected void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.  
  67.         PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
  68.                 this);
  69.  
  70.         if (context == null)
  71.             context = this.getApplicationContext();
  72.  
  73.         settings = context.getSharedPreferences(PREFS_NAME, 0);
  74.         editor = settings.edit();
  75.  
  76.         PreferenceCategory vidCat = new PreferenceCategory(this);
  77.         vidCat.setKey("cat_vid");
  78.         vidCat.setTitle("Video settings");
  79.  
  80.         PreferenceCategory resCat = new PreferenceCategory(this);
  81.         resCat.setKey("cat_0camres");
  82.         resCat.setTitle("Camera settings");
  83.  
  84.         ListPreference cam1 = new ListPreference(this);
  85.         cam1.setTitle("Resolution");
  86.         cam1.setDialogTitle("Resolution");
  87.         cam1.setKey("cam0res");
  88.  
  89.         ListPreference cam1rotation = new ListPreference(this);
  90.         cam1rotation.setTitle("Rotation");
  91.         cam1rotation.setDialogTitle("Rotation");
  92.         cam1rotation.setKey("cam0rot");
  93.         cam1rotation.setEntries(new String[] { "None", "90 deg", "180 deg",
  94.                 "270 deg" });
  95.         cam1rotation.setEntryValues(new String[] { "-1", "90", "180", "270" });
  96.  
  97.         ListPreference cam1focus = new ListPreference(this);
  98.         cam1focus.setTitle("Focus Mode");
  99.         cam1focus.setDialogTitle("Focus Mode");
  100.         cam1focus.setKey("cam0focus");
  101.         cam1focus.setEntries(new String[] { "Default", "INFINITY", "FIXED",
  102.                 "EDOF", "CONTINUOUS_VIDEO" });
  103.         cam1focus.setEntryValues(new String[] { "-1", "infinity", "fixed",
  104.                 "edof", "continuous-video" });
  105.  
  106.         ListPreference cam1colorEffect = new ListPreference(this);
  107.         cam1colorEffect.setTitle("Color Effect");
  108.         cam1colorEffect.setDialogTitle("Color Effect");
  109.         cam1colorEffect.setKey("cam0colorEffect");
  110.         cam1colorEffect.setEntries(new String[] { "Default", "MONO",
  111.                 "NEGATIVE", "SOLARIZE", "SEPIA", "POSTERIZE", "WHITEBOARD",
  112.                 "BLACKBOARD", "AQUA" });
  113.         cam1colorEffect.setEntryValues(new String[] { "-1", "mono", "negative",
  114.                 "solarize", "sepia", "posterize", "whiteboard", "blackboard",
  115.                 "aqua" });
  116.  
  117.         ListPreference cam1sceneMode = new ListPreference(this);
  118.         cam1sceneMode.setTitle("Scene Mode");
  119.         cam1sceneMode.setDialogTitle("Scene Mode");
  120.         cam1sceneMode.setKey("cam0sceneMode");
  121.         cam1sceneMode.setEntries(new String[] { "Default", "AUTO", "ACTION",
  122.                 "PORTRAIT", "LANDSCAPE", "NIGHT", "NIGHT_PORTRAIT", "THEATRE",
  123.                 "BEACH", "SNOW", "SUNSET", "STEADYPHOTO", "FIREWORKS",
  124.                 "SPORTS", "PARTY", "CANDLELIGHT" });
  125.         cam1sceneMode.setEntryValues(new String[] { "-1", "auto", "action",
  126.                 "portrait", "landscape", "night", "night-portrait", "theatre",
  127.                 "beach", "snow", "sunset", "steadyphoto", "fireworks",
  128.                 "sports", "party", "candlelight" });
  129.  
  130.         ListPreference cam1whiteBalance = new ListPreference(this);
  131.         cam1whiteBalance.setTitle("White Balance");
  132.         cam1whiteBalance.setDialogTitle("White Balance");
  133.         cam1whiteBalance.setKey("cam0whiteBalance");
  134.         cam1whiteBalance.setEntries(new String[] { "Default", "AUTO",
  135.                 "INCANDESCENT", "FLUORESCENT", "WARM_FLUORESCENT", "DAYLIGHT",
  136.                 "CLOUDY_DAYLIGHT", "TWILIGHT", "SHADE" });
  137.         cam1whiteBalance.setEntryValues(new String[] { "-1", "auto",
  138.                 "incandescent", "fluorescent", "warm-fluorescent", "daylight",
  139.                 "cloudy-daylight", "twilight", "shade" });
  140.  
  141.         CheckBoxPreference cam1flip = new CheckBoxPreference(this);
  142.         cam1flip.setTitle("Flip camera preview");
  143.         cam1flip.setSummary("Image you see (not image sent!) will be flipped horizontally (mirrored)");
  144.         cam1flip.setKey("cam0flip");
  145.         cam1flip.setChecked(settings.getBoolean("cam0flip", false));
  146.  
  147.         CheckBoxPreference cam1flipAll = new CheckBoxPreference(this);
  148.         cam1flipAll.setTitle("Flip camera image");
  149.         cam1flipAll
  150.                 .setSummary("Both image you see and image sent will be flipped horizontally (mirrored)");
  151.         cam1flipAll.setKey("cam0flipAll");
  152.         cam1flipAll.setChecked(settings.getBoolean("cam0flipAll", false));
  153.  
  154.         PreferenceScreen cam0screen = getPreferenceManager()
  155.                 .createPreferenceScreen(this);
  156.         cam0screen.setKey("cam0screen");
  157.  
  158.         ListPreference cam2 = new ListPreference(this);
  159.         cam2.setTitle("Resolution");
  160.         cam2.setDialogTitle("Resolution");
  161.         cam2.setKey("cam1res");
  162.  
  163.         ListPreference cam2focus = new ListPreference(this);
  164.         cam2focus.setTitle("Focus Mode");
  165.         cam2focus.setDialogTitle("Focus Mode");
  166.         cam2focus.setKey("cam1focus");
  167.         cam2focus.setEntries(new String[] { "Default", "INFINITY", "FIXED",
  168.                 "EDOF", "CONTINUOUS_VIDEO" });
  169.         cam2focus.setEntryValues(new String[] { "-1", "infinity", "fixed",
  170.                 "edof", "continuous-video" });
  171.  
  172.         ListPreference cam2colorEffect = new ListPreference(this);
  173.         cam2colorEffect.setTitle("Color Effect");
  174.         cam2colorEffect.setDialogTitle("Color Effect");
  175.         cam2colorEffect.setKey("cam1colorEffect");
  176.         cam2colorEffect.setEntries(new String[] { "Default", "MONO",
  177.                 "NEGATIVE", "SOLARIZE", "SEPIA", "POSTERIZE", "WHITEBOARD",
  178.                 "BLACKBOARD", "AQUA" });
  179.         cam2colorEffect.setEntryValues(new String[] { "-1", "mono", "negative",
  180.                 "solarize", "sepia", "posterize", "whiteboard", "blackboard",
  181.                 "aqua" });
  182.  
  183.         ListPreference cam2sceneMode = new ListPreference(this);
  184.         cam2sceneMode.setTitle("Scene Mode");
  185.         cam2sceneMode.setDialogTitle("Scene Mode");
  186.         cam2sceneMode.setKey("cam1sceneMode");
  187.         cam2sceneMode.setEntries(new String[] { "Default", "AUTO", "ACTION",
  188.                 "PORTRAIT", "LANDSCAPE", "NIGHT", "NIGHT_PORTRAIT", "THEATRE",
  189.                 "BEACH", "SNOW", "SUNSET", "STEADYPHOTO", "FIREWORKS",
  190.                 "SPORTS", "PARTY", "CANDLELIGHT" });
  191.         cam2sceneMode.setEntryValues(new String[] { "-1", "auto", "action",
  192.                 "portrait", "landscape", "night", "night-portrait", "theatre",
  193.                 "beach", "snow", "sunset", "steadyphoto", "fireworks",
  194.                 "sports", "party", "candlelight" });
  195.  
  196.         ListPreference cam2whiteBalance = new ListPreference(this);
  197.         cam2whiteBalance.setTitle("White Balance");
  198.         cam2whiteBalance.setDialogTitle("White Balance");
  199.         cam2whiteBalance.setKey("cam1whiteBalance");
  200.         cam2whiteBalance.setEntries(new String[] { "Default", "AUTO",
  201.                 "INCANDESCENT", "FLUORESCENT", "WARM_FLUORESCENT", "DAYLIGHT",
  202.                 "CLOUDY_DAYLIGHT", "TWILIGHT", "SHADE" });
  203.         cam2whiteBalance.setEntryValues(new String[] { "-1", "auto",
  204.                 "incandescent", "fluorescent", "warm-fluorescent", "daylight",
  205.                 "cloudy-daylight", "twilight", "shade" });
  206.  
  207.         ListPreference cam2rotation = new ListPreference(this);
  208.         cam2rotation.setTitle("Rotation");
  209.         cam2rotation.setDialogTitle("Rotation");
  210.         cam2rotation.setKey("cam1rot");
  211.         cam2rotation.setEntries(new String[] { "None", "90 deg", "180 deg",
  212.                 "270 deg" });
  213.         cam2rotation.setEntryValues(new String[] { "-1", "90", "180", "270" });
  214.  
  215.         CheckBoxPreference cam2flip = new CheckBoxPreference(this);
  216.         cam2flip.setTitle("Flip camera preview");
  217.         cam2flip.setSummary("Image you see (not image sent!) will be flipped horizontally (mirrored)");
  218.         cam2flip.setKey("cam1flip");
  219.         cam2flip.setChecked(settings.getBoolean("cam1flip", false));
  220.  
  221.         CheckBoxPreference cam2flipAll = new CheckBoxPreference(this);
  222.         cam2flipAll.setTitle("Flip camera image");
  223.         cam2flipAll
  224.                 .setSummary("Both image you see and image sent will be flipped horizontally (mirrored)");
  225.         cam2flipAll.setKey("cam1flipAll");
  226.         cam2flipAll.setChecked(settings.getBoolean("cam1flipAll", false));
  227.  
  228.         PreferenceScreen cam1screen = getPreferenceManager()
  229.                 .createPreferenceScreen(this);
  230.         cam1screen.setKey("cam1screen");
  231.  
  232.         Preference toastCamInfo = new Preference(this);
  233.         toastCamInfo.setTitle("Number of cameras:");
  234.         toastCamInfo.setKey("camInfoButton");
  235.         toastCamInfo
  236.                 .setOnPreferenceClickListener(new OnPreferenceClickListener() {
  237.  
  238.                     @Override
  239.                     public boolean onPreferenceClick(Preference preference) {
  240.                         AdvancedSettings
  241.                                 .toastCameraInfo(AdvancedSettings.context);
  242.                         return false;
  243.                     }
  244.                 });
  245.  
  246.         CheckBoxPreference forceVideo = new CheckBoxPreference(this);
  247.         forceVideo.setTitle("Force-enable video-calling");
  248.         forceVideo.setSummary("Enable video-calling on your device!");
  249.         forceVideo.setKey("forceVideo");
  250.         forceVideo.setChecked(settings.getBoolean("forceVideo", false));
  251.  
  252.         setPreferenceScreen(root);
  253.  
  254.         if ((numCams = settings.getInt("numCams", -99)) == -99) {
  255.             numCams = android.hardware.Camera.getNumberOfCameras();
  256.             Toast.makeText(context, "Found " + numCams + " camera(s)!",
  257.                     Toast.LENGTH_LONG).show();
  258.             editor.putInt("numCams", numCams);
  259.             editor.commit();
  260.         }
  261.  
  262.         toastCamInfo.setTitle("Number of cameras: " + numCams);
  263.  
  264.         // No way it gets in here :P
  265.         if (android.os.Build.VERSION.SDK_INT < 9) {
  266.             Toast.makeText(
  267.                     context,
  268.                     "You're running Android 2.2 or older, I wonder how you got here..",
  269.                     Toast.LENGTH_LONG).show();
  270.         }
  271.  
  272.         Preference buildInfo = new Preference(this);
  273.         buildInfo.setTitle("Developed by theos0o");
  274.         buildInfo.setSummary("Build " + buildNo);
  275.         buildInfo.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  276.  
  277.             @Override
  278.             public boolean onPreferenceClick(Preference preference) {
  279.                 Intent browserIntent = new Intent(
  280.                         Intent.ACTION_VIEW,
  281.                         Uri.parse("http://forum.xda-developers.com/showthread.php?t=1148708"));
  282.                 startActivity(browserIntent);
  283.                 return false;
  284.             }
  285.         });
  286.  
  287.         PreferenceCategory audioCat = new PreferenceCategory(this);
  288.         audioCat.setKey("cat_audio");
  289.         audioCat.setTitle("Audio settings");
  290.  
  291.         CheckBoxPreference customRing = new CheckBoxPreference(this);
  292.         customRing.setKey("useCustomRing");
  293.         customRing.setTitle("Custom ringtone");
  294.         customRing.setSummary("Hate Skype's ringtone? :D");
  295.         customRing.setChecked(settings.getBoolean("useCustomRing", false));
  296.  
  297.         RingtonePreference ringtone = new RingtonePreference(this) {
  298.             @Override
  299.             protected void onSaveRingtone(Uri ringtoneUri) {
  300.                 super.onSaveRingtone(ringtoneUri);
  301.                 context.getSharedPreferences(PREFS_NAME, 0).edit()
  302.                         .putString("ringtoneS", ringtoneUri.toString())
  303.                         .commit();
  304.             }
  305.         };
  306.         ringtone.setTitle("Ringtone");
  307.         ringtone.setKey("ringtoneS");
  308.         ringtone.setEnabled(settings.getBoolean("useCustomRing", false));
  309.         ringtone.setShowSilent(false);
  310.  
  311.         CheckBoxPreference forceSpeakerOff = new CheckBoxPreference(this);
  312.         forceSpeakerOff.setTitle("Force speaker off after calls");
  313.         forceSpeakerOff
  314.                 .setSummary("Speaker stuck enabled after skype-calls? Enable this!");
  315.         forceSpeakerOff.setKey("speakerOff");
  316.         forceSpeakerOff.setChecked(settings.getBoolean("speakerOff", false));
  317.  
  318.         CheckBoxPreference modeInCall = new CheckBoxPreference(this);
  319.         modeInCall.setTitle("Use MODE_IN_CALL");
  320.         modeInCall.setSummary("Audio failing? Try this..");
  321.         modeInCall.setKey("modeInCall");
  322.         modeInCall.setChecked(settings.getBoolean("modeInCall", false));
  323.  
  324.         PreferenceCategory genCat = new PreferenceCategory(this);
  325.         genCat.setKey("cat_gen");
  326.         genCat.setTitle("General settings");
  327.  
  328.         CheckBoxPreference disableProximity = new CheckBoxPreference(this);
  329.         disableProximity.setKey("disableProximity");
  330.         disableProximity.setTitle("Keep screen alive");
  331.         disableProximity
  332.                 .setSummary("Prevent screen from going off when in audio-call");
  333.         disableProximity.setChecked(settings.getBoolean("disableProximity",
  334.                 false));
  335.  
  336.         CheckBoxPreference forceOneCam = new CheckBoxPreference(this);
  337.         forceOneCam.setKey("forceOneCam");
  338.         forceOneCam.setTitle("Force one camera");
  339.         forceOneCam.setSummary("WTF! I don't have 2 cams dude!");
  340.         forceOneCam.setChecked(settings.getBoolean("forceOneCam", false));
  341.  
  342.         CheckBoxPreference avoidSetFPS = new CheckBoxPreference(this);
  343.         avoidSetFPS.setKey("avoidSetFPS");
  344.         avoidSetFPS.setTitle("Avoid setFPS");
  345.         avoidSetFPS.setSummary("Black video? Give this a try..");
  346.         avoidSetFPS.setChecked(settings.getBoolean("avoidSetFPS", false));
  347.  
  348.         CheckBoxPreference avoidProfiling = new CheckBoxPreference(this);
  349.         avoidProfiling.setKey("avoidProfiling");
  350.         avoidProfiling.setTitle("Avoid FPS profiling");
  351.         avoidProfiling
  352.                 .setSummary("If video is zooming after some time, enable this!");
  353.         avoidProfiling.setChecked(settings.getBoolean("avoidProfiling", false));
  354.  
  355.         root.addPreference(buildInfo);
  356.         root.addPreference(genCat);
  357.         root.addPreference(disableProximity);
  358.         root.addPreference(vidCat);
  359.         root.addPreference(toastCamInfo);
  360.         if (numCams >= 1) {
  361.             root.addPreference(forceVideo);
  362.             if (forceVideo() == 1) {
  363.                 if (numCams == 2) {
  364.                     root.addPreference(forceOneCam);
  365.                 }
  366.                 root.addPreference(avoidSetFPS);
  367.                 root.addPreference(avoidProfiling);
  368.                 root.addPreference(resCat);
  369.                 root.addPreference(cam0screen);
  370.                 cam0screen.addPreference(cam1);
  371.                 cam0screen.addPreference(cam1focus);
  372.                 if (colorEffectSupported(0)) {
  373.                     cam0screen.addPreference(cam1colorEffect);
  374.                 }
  375.                 if (sceneModeSupported(0)) {
  376.                     cam0screen.addPreference(cam1sceneMode);
  377.                 }
  378.                 if (whiteBalanceSupported(0)) {
  379.                     cam0screen.addPreference(cam1whiteBalance);
  380.                 }
  381.                 cam0screen.addPreference(cam1rotation);
  382.                 cam0screen.addPreference(cam1flip);
  383.                 cam0screen.addPreference(cam1flipAll);
  384.                 initCamValues(0);
  385.             }
  386.         }
  387.         if (numCams >= 2 && forceVideo() == 1) {
  388.             root.addPreference(cam1screen);
  389.             cam1screen.addPreference(cam2);
  390.             cam1screen.addPreference(cam2focus);
  391.             if (colorEffectSupported(1)) {
  392.                 cam1screen.addPreference(cam2colorEffect);
  393.             }
  394.             if (sceneModeSupported(1)) {
  395.                 cam1screen.addPreference(cam2sceneMode);
  396.             }
  397.             if (whiteBalanceSupported(1)) {
  398.                 cam1screen.addPreference(cam2whiteBalance);
  399.             }
  400.             cam1screen.addPreference(cam2rotation);
  401.             cam1screen.addPreference(cam2flip);
  402.             cam1screen.addPreference(cam2flipAll);
  403.             initCamValues(1);
  404.         }
  405.  
  406.         CheckBoxPreference customNotif = new CheckBoxPreference(this);
  407.         customNotif.setKey("useCustomNotif");
  408.         customNotif.setTitle("Custom notification sound");
  409.         customNotif.setChecked(settings.getBoolean("useCustomNotif", false));
  410.  
  411.         RingtonePreference notifS = new RingtonePreference(this) {
  412.             @Override
  413.             protected void onSaveRingtone(Uri ringtoneUri) {
  414.                 super.onSaveRingtone(ringtoneUri);
  415.                 context.getSharedPreferences(PREFS_NAME, 0).edit()
  416.                         .putString("notifS", ringtoneUri.toString()).commit();
  417.             }
  418.         };
  419.         notifS.setTitle("Notification sound");
  420.         notifS.setKey("notifS");
  421.         notifS.setEnabled(settings.getBoolean("useCustomNotif", false));
  422.         notifS.setShowSilent(false);
  423.         notifS.setRingtoneType(2);
  424.  
  425.         root.addPreference(audioCat);
  426.         root.addPreference(customRing);
  427.         root.addPreference(ringtone);
  428.         root.addPreference(customNotif);
  429.         root.addPreference(notifS);
  430.         root.addPreference(forceSpeakerOff);
  431.         root.addPreference(modeInCall);
  432.  
  433.         for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
  434.             initSummary(getPreferenceScreen().getPreference(i));
  435.         }
  436.     }
  437.  
  438.     @Override
  439.     protected void onResume() {
  440.         super.onResume();
  441.         getPreferenceScreen().getSharedPreferences()
  442.                 .registerOnSharedPreferenceChangeListener(this);
  443.     }
  444.  
  445.     @Override
  446.     protected void onPause() {
  447.         super.onPause();
  448.         getPreferenceScreen().getSharedPreferences()
  449.                 .unregisterOnSharedPreferenceChangeListener(this);
  450.     }
  451.  
  452.     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  453.             String key) {
  454.         updatePrefSummary(findPreference(key));
  455.     }
  456.  
  457.     private void initSummary(Preference p) {
  458.         if (p instanceof PreferenceCategory) {
  459.             PreferenceCategory pCat = (PreferenceCategory) p;
  460.             for (int i = 0; i < pCat.getPreferenceCount(); i++) {
  461.                 initSummary(pCat.getPreference(i));
  462.             }
  463.         } else if (p instanceof PreferenceScreen) {
  464.             PreferenceScreen pScreen = (PreferenceScreen) p;
  465.             for (int i = 0; i < pScreen.getPreferenceCount(); i++) {
  466.                 initSummary(pScreen.getPreference(i));
  467.             }
  468.         } else {
  469.             if (!(p instanceof CheckBoxPreference))
  470.                 updatePrefSummary(p);
  471.         }
  472.  
  473.     }
  474.  
  475.     private void updatePrefSummary(Preference p) {
  476.         if (p instanceof ListPreference) {
  477.             ListPreference listPref = (ListPreference) p;
  478.             p.setSummary(listPref.getEntry());
  479.             editor.putString(p.getKey(), ((ListPreference) p).getValue());
  480.             editor.commit();
  481.         } else if (p instanceof EditTextPreference) {
  482.             EditTextPreference editTextPref = (EditTextPreference) p;
  483.             p.setSummary(editTextPref.getText());
  484.         } else if (p instanceof CheckBoxPreference) {
  485.             if (p.getKey().equals("useCustomRing")) {
  486.                 findPreference("ringtoneS").setEnabled(
  487.                         ((CheckBoxPreference) p).isChecked());
  488.             } else if (p.getKey().equals("useCustomNotif")) {
  489.                 findPreference("notifS").setEnabled(
  490.                         ((CheckBoxPreference) p).isChecked());
  491.             }
  492.             editor.putBoolean(p.getKey(), ((CheckBoxPreference) p).isChecked());
  493.             editor.commit();
  494.             if (p.getKey().equals("forceVideo")
  495.                     || p.getKey().equals("disableProximity")) {
  496.                 promptRestartApp();
  497.             }
  498.         }
  499.     }
  500.  
  501.     public void initCamValues(int camID) {
  502.         ListPreference targetCam = (ListPreference) findPreference("cam"
  503.                 + (camID) + "res");
  504.         ArrayList<String> choices = new ArrayList<String>();
  505.         ArrayList<String> values = new ArrayList<String>();
  506.         choices.add("Let Skype decide");
  507.         values.add("-1");
  508.         choices.add("Force 320x240");
  509.         values.add("-2");
  510.         choices.add("Force 176x144");
  511.         values.add("-3");
  512.         for (Size tmp : getSupportedSizes(camID)) {
  513.             choices.add(tmp.width + "x" + tmp.height);
  514.             values.add(tmp.width + "x" + tmp.height);
  515.         }
  516.         targetCam.setEntries(choices.toArray(new CharSequence[choices.size()]));
  517.         targetCam
  518.                 .setEntryValues(values.toArray(new CharSequence[values.size()]));
  519.         String index = settings.getString("cam" + (camID) + "res", "-1");
  520.         targetCam.setValue(index);
  521.         targetCam.setSummary(targetCam.getEntry());
  522.         ((PreferenceScreen) findPreference("cam" + camID + "screen"))
  523.                 .setTitle((getCameraFacing(camID) == CameraInfo.CAMERA_FACING_BACK ? "Back camera"
  524.                         : "Front camera"));
  525.  
  526.         ListPreference targetRotation = ((ListPreference) findPreference("cam"
  527.                 + camID + "rot"));
  528.         index = settings.getString("cam" + (camID) + "rot", "-1");
  529.         targetRotation.setValue(index);
  530.         targetRotation.setSummary(targetRotation.getEntry());
  531.  
  532.         ListPreference targetFocus = ((ListPreference) findPreference("cam"
  533.                 + camID + "focus"));
  534.         index = settings.getString("cam" + (camID) + "focus", "-1");
  535.         targetFocus.setValue(index);
  536.         targetFocus.setSummary(targetFocus.getEntry());
  537.  
  538.         if (colorEffectSupported(camID)) {
  539.             ListPreference targetColor = ((ListPreference) findPreference("cam"
  540.                     + camID + "colorEffect"));
  541.             index = settings.getString("cam" + (camID) + "colorEffect", "-1");
  542.             targetColor.setValue(index);
  543.             targetColor.setSummary(targetColor.getEntry());
  544.         }
  545.  
  546.         if (sceneModeSupported(camID)) {
  547.             ListPreference targetScene = ((ListPreference) findPreference("cam"
  548.                     + camID + "sceneMode"));
  549.             index = settings.getString("cam" + (camID) + "sceneMode", "-1");
  550.             targetScene.setValue(index);
  551.             targetScene.setSummary(targetScene.getEntry());
  552.         }
  553.  
  554.         if (whiteBalanceSupported(camID)) {
  555.             ListPreference targetBalance = ((ListPreference) findPreference("cam"
  556.                     + camID + "whiteBalance"));
  557.             index = settings.getString("cam" + (camID) + "whiteBalance", "-1");
  558.             targetBalance.setValue(index);
  559.             targetBalance.setSummary(targetBalance.getEntry());
  560.         }
  561.     }
  562.  
  563.     public static List<Size> getSupportedSizes(int camID) {
  564.         if (camID >= Camera.getNumberOfCameras()) {
  565.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  566.                     + ")");
  567.         }
  568.         Camera targetCam = Camera.open(camID);
  569.         List<Size> tmp = targetCam.getParameters().getSupportedPreviewSizes();
  570.         targetCam.release();
  571.         return tmp;
  572.     }
  573.  
  574.     public static void toastCameraInfo(Context context) {
  575.         int i;
  576.         Toast.makeText(context,
  577.                 "You're running Android " + android.os.Build.VERSION.RELEASE,
  578.                 Toast.LENGTH_LONG).show();
  579.         for (i = 0; i < Camera.getNumberOfCameras(); i++) {
  580.             CameraInfo temp = new CameraInfo();
  581.             Camera.getCameraInfo(i, temp);
  582.             String tmp = "";
  583.             tmp += "CID: "
  584.                     + i
  585.                     + ", FACING: "
  586.                     + (temp.facing == Camera.CameraInfo.CAMERA_FACING_BACK ? "BACK"
  587.                             : "FRONT") + ", ORIENTATION: " + temp.orientation;
  588.             Toast.makeText(context, tmp, Toast.LENGTH_LONG).show();
  589.         }
  590.     }
  591.  
  592.     public static int numberOfCams() {
  593.         if (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  594.                 "forceOneCam", false)) {
  595.             return 1;
  596.         }
  597.         return Camera.getNumberOfCameras();
  598.     }
  599.  
  600.     public static int getCameraFacing(int camID) {
  601.         Log.d("getCameraFacing", "Got camID = " + camID);
  602.         if (camID >= Camera.getNumberOfCameras()) {
  603.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  604.                     + ")");
  605.         }
  606.         CameraInfo temp = new CameraInfo();
  607.         Camera.getCameraInfo(camID, temp);
  608.         return temp.facing;
  609.     }
  610.  
  611.     public static int getCamResIndex(int camID) {
  612.         Log.d("getCamResIndex", "Got camID = " + camID);
  613.         if (camID >= Camera.getNumberOfCameras()) {
  614.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  615.                     + ")");
  616.         }
  617.         String index = context.getSharedPreferences(PREFS_NAME, 0).getString(
  618.                 "cam" + (camID) + "res", "-1");
  619.         try {
  620.             Log.d("getCamResIndex", "Returning " + Integer.parseInt(index));
  621.             return Integer.parseInt(index);
  622.         } catch (NumberFormatException e) {
  623.             Log.d("getCamResIndex", "NumberFormatException (" + index
  624.                     + "), returning -99");
  625.             return -99;
  626.         }
  627.     }
  628.  
  629.     public static int[] getPrefSizeArray(int camID) {
  630.         Log.d("getPrefSizeArray", "Got camID = " + camID);
  631.         String index = context.getSharedPreferences(PREFS_NAME, 0).getString(
  632.                 "cam" + (camID) + "res", "-1");
  633.         String[] splitValue = index.split("x");
  634.         int[] rval = { Integer.parseInt(splitValue[0]),
  635.                 Integer.parseInt(splitValue[1]) };
  636.         Log.d("getPrefSizeArray",
  637.                 "Returning " + Integer.parseInt(splitValue[0]) + "x"
  638.                         + Integer.parseInt(splitValue[1]));
  639.         return rval;
  640.     }
  641.  
  642.     public static int getFlipValue(int camID) {
  643.         Boolean boolval = context.getSharedPreferences(PREFS_NAME, 0)
  644.                 .getBoolean("cam" + (camID) + "flip", false);
  645.         if (camID == 0) {
  646.             if (boolval)
  647.                 return 1;
  648.             else
  649.                 return 0;
  650.         } else {
  651.             if (boolval)
  652.                 return 0;
  653.             else
  654.                 return 1;
  655.         }
  656.     }
  657.  
  658.     public static int getRotateValue(int camID) {
  659.         int intval = -1;
  660.         try {
  661.             intval = Integer.parseInt(context.getSharedPreferences(PREFS_NAME,
  662.                     0).getString("cam" + (camID) + "rot", "-1"));
  663.         } catch (NumberFormatException e) {
  664.             intval = -1;
  665.         }
  666.         if (intval != -1)
  667.             return intval;
  668.         else
  669.             return getCameraOrientation(camID);
  670.     }
  671.  
  672.     private static int getCameraOrientation(int camID) {
  673.         Log.d("getRotation", "Got camID = " + camID);
  674.         if (camID >= Camera.getNumberOfCameras()) {
  675.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  676.                     + ")");
  677.         }
  678.         CameraInfo temp = new CameraInfo();
  679.         Camera.getCameraInfo(camID, temp);
  680.         return temp.orientation;
  681.     }
  682.  
  683.     public static void initContextVar(Context context) {
  684.         AdvancedSettings.context = context;
  685.     }
  686.  
  687.     public static int forceVideo() {
  688.         return (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  689.                 "forceVideo", false) ? 1 : 0);
  690.     }
  691.  
  692.     public static void audioMode(AudioManager am, int close) {
  693.         AdvancedSettings.am = am;
  694.         if (close == 1 && AdvancedSettings.bluetoothAudioOn){
  695.             am.setBluetoothScoOn(false);
  696.             am.stopBluetoothSco();
  697.             am.setMicrophoneMute(false);
  698.             am.setSpeakerphoneOn(false);
  699.             AdvancedSettings.bluetoothAudioOn = false;
  700.         }
  701.         if (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  702.                 "modeInCall", false)) {
  703.             if (close == 0) {
  704.                 am.setMode(AudioManager.MODE_IN_CALL);
  705.             } else {
  706.                 am.setMode(AudioManager.MODE_NORMAL);
  707.             }
  708.         }
  709.     }
  710.  
  711.     public static void forceSpeakerOff(AudioManager am) {
  712.         if (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  713.                 "speakerOff", false)) {
  714.             am.setSpeakerphoneOn(false);
  715.         }
  716.     }
  717.  
  718.     public static void setFocus(int camID, Camera.Parameters params) {
  719.         String strval = (context.getSharedPreferences(PREFS_NAME, 0).getString(
  720.                 "cam" + camID + "focus", "-1"));
  721.         if (!strval.equals("-1")) {
  722.             params.setFocusMode(strval);
  723.         }
  724.     }
  725.  
  726.     public static Boolean colorEffectSupported(int camID) {
  727.         if (camID >= Camera.getNumberOfCameras()) {
  728.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  729.                     + ")");
  730.         }
  731.         Camera targetCam = Camera.open(camID);
  732.         String tmp = targetCam.getParameters().getColorEffect();
  733.         targetCam.release();
  734.         return (tmp != null);
  735.     }
  736.  
  737.     public static void setColorEffect(int camID, Camera.Parameters params) {
  738.         String strval = (context.getSharedPreferences(PREFS_NAME, 0).getString(
  739.                 "cam" + camID + "colorEffect", "-1"));
  740.         if (!strval.equals("-1")) {
  741.             params.setColorEffect(strval);
  742.         }
  743.     }
  744.  
  745.     public static Boolean sceneModeSupported(int camID) {
  746.         if (camID >= Camera.getNumberOfCameras()) {
  747.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  748.                     + ")");
  749.         }
  750.         Camera targetCam = Camera.open(camID);
  751.         String tmp = targetCam.getParameters().getSceneMode();
  752.         targetCam.release();
  753.         return (tmp != null);
  754.     }
  755.  
  756.     public static void setSceneMode(int camID, Camera.Parameters params) {
  757.         String strval = (context.getSharedPreferences(PREFS_NAME, 0).getString(
  758.                 "cam" + camID + "sceneMode", "-1"));
  759.         if (!strval.equals("-1")) {
  760.             params.setSceneMode(strval);
  761.         }
  762.     }
  763.  
  764.     public static Boolean whiteBalanceSupported(int camID) {
  765.         if (camID >= Camera.getNumberOfCameras()) {
  766.             throw new IllegalArgumentException("Invalid camera ID (" + camID
  767.                     + ")");
  768.         }
  769.         Camera targetCam = Camera.open(camID);
  770.         String tmp = targetCam.getParameters().getWhiteBalance();
  771.         targetCam.release();
  772.         return (tmp != null);
  773.     }
  774.  
  775.     public static void setWhiteBalance(int camID, Camera.Parameters params) {
  776.         String strval = (context.getSharedPreferences(PREFS_NAME, 0).getString(
  777.                 "cam" + camID + "whiteBalance", "-1"));
  778.         if (!strval.equals("-1")) {
  779.             params.setWhiteBalance(strval);
  780.         }
  781.     }
  782.  
  783.     public static void traceToast() {
  784.         if (context != null)
  785.             Toast.makeText(context, "I am here!", Toast.LENGTH_LONG).show();
  786.         Log.d("traceToast", "I am here!");
  787.         Thread.dumpStack();
  788.     }
  789.  
  790.     public static void exitButton(Service svc) {
  791.         svc.stopForeground(true);
  792.         svc.stopSelf();
  793.         System.exit(0);
  794.     }
  795.  
  796.     public void promptRestartApp() {
  797.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  798.         builder.setMessage(
  799.                 "You need to kill and restart Skype for this change to take effect!")
  800.                 .setCancelable(false)
  801.                 .setPositiveButton("Restart Skype",
  802.                         new DialogInterface.OnClickListener() {
  803.                             public void onClick(DialogInterface dialog, int id) {
  804.                                 killApp();
  805.                             }
  806.                         })
  807.                 .setNegativeButton("Later!",
  808.                         new DialogInterface.OnClickListener() {
  809.                             public void onClick(DialogInterface dialog, int id) {
  810.                                 dialog.cancel();
  811.                             }
  812.                         });
  813.         AlertDialog alert = builder.create();
  814.         alert.show();
  815.     }
  816.  
  817.     public void killApp() {
  818.         android.os.Process.killProcess(android.os.Process.myPid());
  819.     }
  820.  
  821.     public static Uri getRingtoneUri(Uri in) {
  822.         try {
  823.             String[] splitURI = in.toString().split("/");
  824.             String resName = context.getResources().getResourceName(
  825.                     Integer.parseInt(splitURI[splitURI.length - 1]));
  826.             if (resName.contains("raw") && resName.contains("incoming_call")) { // raw/incoming_call.wav
  827.                 if (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  828.                         "useCustomRing", false)) {
  829.                     return Uri.parse(context
  830.                             .getSharedPreferences(PREFS_NAME, 0).getString(
  831.                                     "ringtoneS",
  832.                                     Settings.System.DEFAULT_RINGTONE_URI
  833.                                             .toString()));
  834.                 } else {
  835.                     return in;
  836.                 }
  837.             } else if (resName.contains("raw")
  838.                     && resName.contains("message_received")) { // raw/message_received.wav
  839.                 if (context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  840.                         "useCustomNotif", false)) {
  841.                     return Uri.parse(context
  842.                             .getSharedPreferences(PREFS_NAME, 0).getString(
  843.                                     "notifS",
  844.                                     Settings.System.DEFAULT_NOTIFICATION_URI
  845.                                             .toString()));
  846.                 } else {
  847.                     return in;
  848.                 }
  849.             } else {
  850.                 return in;
  851.             }
  852.         } catch (NumberFormatException e) {
  853.             return in;
  854.         }
  855.     }
  856.  
  857.     public static void setParams(int camID, Camera.Parameters params) {
  858.         lastCamID = camID;
  859.         setFocus(camID, params);
  860.         setColorEffect(camID, params);
  861.         setSceneMode(camID, params);
  862.         setWhiteBalance(camID, params);
  863.     }
  864.  
  865.     public static String disableProximityVal() {
  866.         Boolean boolval = context.getSharedPreferences(PREFS_NAME, 0)
  867.                 .getBoolean("disableProximity", false);
  868.         return (boolval ? "WAKE_BIT_SCREEN_BRIGHT"
  869.                 : "PROXIMITY_SCREEN_OFF_WAKE_LOCK");
  870.     }
  871.  
  872.     public static int avoidSetFPS() {
  873.         Boolean boolval = context.getSharedPreferences(PREFS_NAME, 0)
  874.                 .getBoolean("avoidSetFPS", false);
  875.         return (boolval ? 1 : 0);
  876.     }
  877.  
  878.     public static int avoidProfiling() {
  879.         Boolean boolval = context.getSharedPreferences(PREFS_NAME, 0)
  880.                 .getBoolean("avoidProfiling", false);
  881.         return (boolval ? 1 : 0);
  882.     }
  883.  
  884.     public static void showAdvancedSettings(Activity activity) {
  885.         Intent show = new Intent(context, AdvancedSettings.class);
  886.         activity.startActivity(show);
  887.     }
  888.  
  889.     public static void onPreviewFrame(byte[] data, Camera camera, int camID) {
  890.         int rotation = -1;
  891.         try {
  892.             rotation = Integer.parseInt(context.getSharedPreferences(
  893.                     PREFS_NAME, 0).getString("cam" + camID + "rot", "-1"));
  894.         } catch (NumberFormatException e) {
  895.             rotation = -1;
  896.         }
  897.         if (rotation != -1) {
  898.             Size size = camera.getParameters().getPreviewSize();
  899.             YuvImage yuv = new YuvImage(data, ImageFormat.NV21, size.width,
  900.                     size.height, null);
  901.             ByteArrayOutputStream stream = new ByteArrayOutputStream();
  902.             yuv.compressToJpeg(new Rect(0, 0, size.width, size.height), 50,
  903.                     stream);
  904.             Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0,
  905.                     stream.size());
  906.             Bitmap scaledBitmap;
  907.             scaledBitmap = Bitmap.createBitmap(size.width, size.height,
  908.                     Config.RGB_565);
  909.             scaleBitmap(bmp, scaledBitmap, rotation);
  910.             bmp.recycle();
  911.             convertBitmapToNV21(scaledBitmap, data);
  912.             scaledBitmap.recycle();
  913.         }
  914.     }
  915.  
  916.     public static byte[] convertBitmapToNV21(Bitmap bmp, byte[] yuv) {
  917.         int width = bmp.getWidth();
  918.         int height = bmp.getHeight();
  919.         int[] argb = new int[width * height];
  920.         int yuv_size = width * height
  921.                 * ImageFormat.getBitsPerPixel(ImageFormat.NV21);
  922.         yuv_size = (yuv_size / 8) + ((yuv_size % 8) == 0 ? 0 : 1);
  923.         if (yuv == null || yuv.length < yuv_size)
  924.             yuv = new byte[yuv_size];
  925.         bmp.getPixels(argb, 0, width, 0, 0, width, height);
  926.         int vuStart = width * height;
  927.         byte[] yuvColor = new byte[3];
  928.         for (int row = 0; row < height; ++row) {
  929.             for (int col = 0; col < width; ++col) {
  930.                 int idx = row * width + col;
  931.                 argb2yuv(argb[idx], yuvColor);
  932.                 yuv[idx] = yuvColor[0];
  933.                 if ((row & 1) == 0 && (col & 1) == 0) {
  934.                     int offset = row / 2 * width + col / 2 * 2;
  935.                     yuv[vuStart + offset] = yuvColor[2];
  936.                     yuv[vuStart + offset + 1] = yuvColor[1];
  937.                 }
  938.             }
  939.         }
  940.         return yuv;
  941.     }
  942.  
  943.     private static final int CSHIFT = 16;
  944.     private static final int CYR = 19595;
  945.     private static final int CYG = 38470;
  946.     private static final int CYB = 7471;
  947.     private static final int CUR = -11059;
  948.     private static final int CUG = -21709;
  949.     private static final int CUB = 32768;
  950.     private static final int CVR = 32768;
  951.     private static final int CVG = -27439;
  952.     private static final int CVB = -5329;
  953.  
  954.     private static void argb2yuv(int argb, byte[] yuv) {
  955.         int r = Color.red(argb);
  956.         int g = Color.green(argb);
  957.         int b = Color.blue(argb);
  958.         yuv[0] = (byte) ((CYR * r + CYG * g + CYB * b) >> CSHIFT);
  959.         yuv[1] = (byte) (((CUR * r + CUG * g + CUB * b) >> CSHIFT) + 128);
  960.         yuv[2] = (byte) (((CVR * r + CVG * g + CVB * b) >> CSHIFT) + 128);
  961.     }
  962.  
  963.     public static Bitmap scaleBitmap(Bitmap unscaledBitmap,
  964.             Bitmap scaledBitmap, int degrees) {
  965.         Canvas canvas = new Canvas(scaledBitmap);
  966.         float ratioX, ratioY, offsetX = 0f, offsetY = 0f;
  967.         int dstWidth = scaledBitmap.getWidth(), dstHeight = scaledBitmap
  968.                 .getHeight();
  969.         int srcWidth = unscaledBitmap.getWidth(), srcHeight = unscaledBitmap
  970.                 .getHeight();
  971.         if (((degrees %= 360) % 180) == 90) {
  972.             int i = srcWidth;
  973.             srcWidth = srcHeight;
  974.             srcHeight = i;
  975.         }
  976.         canvas.drawColor(Color.BLACK);
  977.         ratioX = ratioY = Math.min((float) dstWidth / (float) srcWidth,
  978.                 (float) dstHeight / (float) srcHeight);
  979.         offsetX = (((float) srcWidth * ratioX) - (float) dstWidth) / 2f;
  980.         offsetY = (((float) srcHeight * ratioY) - (float) dstHeight) / 2f;
  981.         if (degrees == 0)
  982.             canvas.translate(-offsetX, -offsetY); // for 0
  983.         else if (degrees == 90)
  984.             canvas.translate(dstWidth + offsetX, offsetY); // for 90
  985.         else if (degrees == 180)
  986.             canvas.translate(dstWidth - offsetX, dstHeight - offsetY); // for
  987.                                                                         // 180
  988.         else if (degrees == 270)
  989.             canvas.translate(dstWidth / 2 + 2 * offsetX, dstHeight - offsetY); // for
  990.                                                                                 // 270
  991.         canvas.scale(ratioX, ratioY);
  992.         if (degrees != 0)
  993.             canvas.rotate(degrees);
  994.         canvas.drawBitmap(unscaledBitmap, 0, 0, new Paint(
  995.                 Paint.FILTER_BITMAP_FLAG));
  996.         return scaledBitmap;
  997.     }
  998.  
  999.     public static boolean getFlipAllValue(int camID) {
  1000.         boolean rval = context.getSharedPreferences(PREFS_NAME, 0).getBoolean(
  1001.                 "cam" + camID + "flipAll", false);
  1002.         return rval;
  1003.     }
  1004.  
  1005.     public static boolean btAudio(MenuItem item) {
  1006.         if (!item.getTitle().toString().contains("BT Audio"))
  1007.             return false;
  1008.         if (!android.bluetooth.BluetoothAdapter.getDefaultAdapter().isEnabled()) {
  1009.             Toast.makeText(context, "Bluetooth is disabled!", 1).show();
  1010.             return true;
  1011.         }
  1012.         AdvancedSettings.bluetoothAudioOn = !AdvancedSettings.bluetoothAudioOn;
  1013.         if (!AdvancedSettings.bluetoothAudioOn) {
  1014.             AdvancedSettings.am.setBluetoothScoOn(false);
  1015.             AdvancedSettings.am.stopBluetoothSco();
  1016.             AdvancedSettings.am.setSpeakerphoneOn(AdvancedSettings.speakerPhoneOn);
  1017.             Toast.makeText(context, "Bluetooth audio is OFF!", 1).show();
  1018.         } else {
  1019.             AdvancedSettings.am.startBluetoothSco();
  1020.             AdvancedSettings.am.setBluetoothScoOn(true);
  1021.             AdvancedSettings.speakerPhoneOn = am.isSpeakerphoneOn();
  1022.             AdvancedSettings.am.setSpeakerphoneOn(false);
  1023.             Toast.makeText(context, "Bluetooth audio is ON!", 1).show();
  1024.         }
  1025.         return true;
  1026.     }
  1027. }
Advertisement
Add Comment
Please, Sign In to add comment