Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextHome.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.contexthome;
  23. import android.app.Activity;
  24. import android.os.Bundle;
  25. import android.os.Handler;
  26. import android.util.Log;
  27. import android.view.View;
  28. import android.widget.LinearLayout;
  29. import java.util.Arrays;
  30. import uk.co.md87.android.contexthome.contexts.*;
  31. import uk.co.md87.android.contexthome.modules.*;
  32. /**
  33. * A home screen that displays e-mails, text messages, etc, and responds to
  34. * the user's current context.
  35. *
  36. * @author chris
  37. */
  38. public class ContextHome extends Activity implements Runnable {
  39. public static int TAG_SCORE = 10;
  40. private LinearLayout layout, fixedLayout;
  41. private ContextType[] contexts;
  42. private DataHelper helper;
  43. private int previousCount = 0;
  44. private Module[] modules, fixedModules;
  45. private final Handler handler = new Handler();
  46. /** Called when the activity is first created. */
  47. @Override
  48. public void onCreate(Bundle icicle) {
  49. super.onCreate(icicle);
  50. contexts = new ContextType[]{
  51. new GlobalContext(), new HourContext(), new PeriodContext(),
  52. new ActivityContext(this), new DestinationContext(this),
  53. new LocationContext(this)
  54. };
  55. helper = new DataHelper(this, Arrays.asList(contexts));
  56. fixedModules = new Module[] {
  57. new ContactsModule(helper), new AppsModule(helper)
  58. };
  59. modules = new Module[]{
  60. new EmailModule(helper), new SmsModule(helper),
  61. };
  62. setContentView(R.layout.container);
  63. layout = (LinearLayout) findViewById(R.id.content);
  64. fixedLayout = (LinearLayout) findViewById(R.id.fixedcontent);
  65. initLayout();
  66. }
  67. private void initLayout() {
  68. fixedLayout.removeAllViews();
  69. layout.removeAllViews();
  70. long start, end;
  71. for (Module module : fixedModules) {
  72. start = System.currentTimeMillis();
  73. module.addViews(fixedLayout, this, 1);
  74. end = System.currentTimeMillis();
  75. Log.v("ContextHome", module.getClass().getSimpleName() + ": " + (end - start));
  76. }
  77. for (Module module : modules) {
  78. start = System.currentTimeMillis();
  79. module.addViews(layout, this, 10);
  80. end = System.currentTimeMillis();
  81. Log.v("ContextHome", module.getClass().getSimpleName() + ": " + (end - start));
  82. }
  83. handler.postDelayed(this, 1000);
  84. }
  85. public boolean sortModules() {
  86. boolean changed = false;
  87. int lastScore = Integer.MAX_VALUE;
  88. for (int i = 0; i < layout.getChildCount(); i++) {
  89. final View view = layout.getChildAt(i);
  90. if (view.getTag(R.id.score) == null) {
  91. Log.w("ContextHome", "Removing view without score");
  92. layout.removeViewAt(i);
  93. i--;
  94. continue;
  95. }
  96. final int score = (Integer) view.getTag(R.id.score);
  97. while (score > lastScore) {
  98. // TODO: This could be optimised quite a lot
  99. changed = true;
  100. layout.removeViewAt(i);
  101. layout.addView(view, --i);
  102. lastScore = i == 0 ? Integer.MAX_VALUE
  103. : (Integer) layout.getChildAt(i - 1).getTag(R.id.score);
  104. }
  105. lastScore = (Integer) layout.getChildAt(i).getTag(R.id.score);
  106. }
  107. changed |= layout.getChildCount() > previousCount;
  108. previousCount = layout.getChildCount();
  109. return changed;
  110. }
  111. public void run() {
  112. if (sortModules()) {
  113. handler.postDelayed(this, 1000);
  114. }
  115. }
  116. }