| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.app.ActivityManager; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.pm.ActivityInfo; |
| 26 |
import android.content.pm.PackageManager; |
| 27 |
import android.content.pm.ResolveInfo; |
| 28 |
import android.database.DataSetObservable; |
| 29 |
import android.os.AsyncTask; |
| 30 |
import android.os.Build; |
| 31 |
import android.text.TextUtils; |
| 32 |
import android.util.Log; |
| 33 |
import android.util.Xml; |
| 34 |
|
| 35 |
import com.android.internal.content.PackageMonitor; |
| 36 |
|
| 37 |
import org.xmlpull.v1.XmlPullParser; |
| 38 |
import org.xmlpull.v1.XmlPullParserException; |
| 39 |
import org.xmlpull.v1.XmlSerializer; |
| 40 |
|
| 41 |
import java.io.FileInputStream; |
| 42 |
import java.io.FileNotFoundException; |
| 43 |
import java.io.FileOutputStream; |
| 44 |
import java.io.IOException; |
| 45 |
import java.math.BigDecimal; |
| 46 |
import java.nio.charset.StandardCharsets; |
| 47 |
import java.util.ArrayList; |
| 48 |
import java.util.Collections; |
| 49 |
import java.util.HashMap; |
| 50 |
import java.util.List; |
| 51 |
import java.util.Map; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public class ActivityChooserModel extends DataSetObservable { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public interface ActivityChooserModelClient { |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void setActivityChooserModel(ActivityChooserModel dataModel); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public interface ActivitySorter { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void sort(Intent intent, List<ActivityResolveInfo> activities, |
| 133 |
List<HistoricalRecord> historicalRecords); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public interface OnChooseActivityListener { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public boolean onChooseActivity(ActivityChooserModel host, Intent intent); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private static final boolean DEBUG = false; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private static final String LOG_TAG = ActivityChooserModel.class.getSimpleName(); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private static final String TAG_HISTORICAL_RECORDS = "historical-records"; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private static final String TAG_HISTORICAL_RECORD = "historical-record"; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private static final String ATTRIBUTE_ACTIVITY = "activity"; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private static final String ATTRIBUTE_TIME = "time"; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private static final String ATTRIBUTE_WEIGHT = "weight"; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public static final String DEFAULT_HISTORY_FILE_NAME = |
| 199 |
"activity_choser_model_history.xml"; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static final int DEFAULT_HISTORY_MAX_LENGTH = 50; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private static final int DEFAULT_ACTIVITY_INFLATION = 5; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
private static final float DEFAULT_HISTORICAL_RECORD_WEIGHT = 1.0f; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private static final String HISTORY_FILE_EXTENSION = ".xml"; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private static final int INVALID_INDEX = -1; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private static final Object sRegistryLock = new Object(); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
private static final Map<String, ActivityChooserModel> sDataModelRegistry = |
| 235 |
new HashMap<String, ActivityChooserModel>(); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private final Object mInstanceLock = new Object(); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private final List<ActivityResolveInfo> mActivities = new ArrayList<ActivityResolveInfo>(); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private final List<HistoricalRecord> mHistoricalRecords = new ArrayList<HistoricalRecord>(); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private final PackageMonitor mPackageMonitor = new DataModelPackageMonitor(); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
private final Context mContext; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
private final String mHistoryFileName; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private Intent mIntent; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
private ActivitySorter mActivitySorter = new DefaultSorter(); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private int mHistoryMaxSize = DEFAULT_HISTORY_MAX_LENGTH; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private boolean mCanReadHistoricalData = true; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private boolean mReadShareHistoryCalled = false; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
private boolean mHistoricalRecordsChanged = true; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
private boolean mReloadActivities = false; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private OnChooseActivityListener mActivityChoserModelPolicy; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
@UnsupportedAppUsage |
| 346 |
public static ActivityChooserModel get(Context context, String historyFileName) { |
| 347 |
synchronized (sRegistryLock) { |
| 348 |
ActivityChooserModel dataModel = sDataModelRegistry.get(historyFileName); |
| 349 |
if (dataModel == null) { |
| 350 |
dataModel = new ActivityChooserModel(context, historyFileName); |
| 351 |
sDataModelRegistry.put(historyFileName, dataModel); |
| 352 |
} |
| 353 |
return dataModel; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
private ActivityChooserModel(Context context, String historyFileName) { |
| 364 |
mContext = context.getApplicationContext(); |
| 365 |
if (!TextUtils.isEmpty(historyFileName) |
| 366 |
&& !historyFileName.endsWith(HISTORY_FILE_EXTENSION)) { |
| 367 |
mHistoryFileName = historyFileName + HISTORY_FILE_EXTENSION; |
| 368 |
} else { |
| 369 |
mHistoryFileName = historyFileName; |
| 370 |
} |
| 371 |
mPackageMonitor.register(mContext, null, true); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 384 |
public void setIntent(Intent intent) { |
| 385 |
synchronized (mInstanceLock) { |
| 386 |
if (mIntent == intent) { |
| 387 |
return; |
| 388 |
} |
| 389 |
mIntent = intent; |
| 390 |
mReloadActivities = true; |
| 391 |
ensureConsistentState(); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public Intent getIntent() { |
| 401 |
synchronized (mInstanceLock) { |
| 402 |
return mIntent; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
@UnsupportedAppUsage |
| 414 |
public int getActivityCount() { |
| 415 |
synchronized (mInstanceLock) { |
| 416 |
ensureConsistentState(); |
| 417 |
return mActivities.size(); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
@UnsupportedAppUsage |
| 430 |
public ResolveInfo getActivity(int index) { |
| 431 |
synchronized (mInstanceLock) { |
| 432 |
ensureConsistentState(); |
| 433 |
return mActivities.get(index).resolveInfo; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public int getActivityIndex(ResolveInfo activity) { |
| 445 |
synchronized (mInstanceLock) { |
| 446 |
ensureConsistentState(); |
| 447 |
List<ActivityResolveInfo> activities = mActivities; |
| 448 |
final int activityCount = activities.size(); |
| 449 |
for (int i = 0; i < activityCount; i++) { |
| 450 |
ActivityResolveInfo currentActivity = activities.get(i); |
| 451 |
if (currentActivity.resolveInfo == activity) { |
| 452 |
return i; |
| 453 |
} |
| 454 |
} |
| 455 |
return INVALID_INDEX; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
@UnsupportedAppUsage |
| 478 |
public Intent chooseActivity(int index) { |
| 479 |
synchronized (mInstanceLock) { |
| 480 |
if (mIntent == null) { |
| 481 |
return null; |
| 482 |
} |
| 483 |
|
| 484 |
ensureConsistentState(); |
| 485 |
|
| 486 |
ActivityResolveInfo chosenActivity = mActivities.get(index); |
| 487 |
|
| 488 |
ComponentName chosenName = new ComponentName( |
| 489 |
chosenActivity.resolveInfo.activityInfo.packageName, |
| 490 |
chosenActivity.resolveInfo.activityInfo.name); |
| 491 |
|
| 492 |
Intent choiceIntent = new Intent(mIntent); |
| 493 |
choiceIntent.setComponent(chosenName); |
| 494 |
|
| 495 |
if (mActivityChoserModelPolicy != null) { |
| 496 |
|
| 497 |
Intent choiceIntentCopy = new Intent(choiceIntent); |
| 498 |
final boolean handled = mActivityChoserModelPolicy.onChooseActivity(this, |
| 499 |
choiceIntentCopy); |
| 500 |
if (handled) { |
| 501 |
return null; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
HistoricalRecord historicalRecord = new HistoricalRecord(chosenName, |
| 506 |
System.currentTimeMillis(), DEFAULT_HISTORICAL_RECORD_WEIGHT); |
| 507 |
addHisoricalRecord(historicalRecord); |
| 508 |
|
| 509 |
return choiceIntent; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 519 |
public void setOnChooseActivityListener(OnChooseActivityListener listener) { |
| 520 |
synchronized (mInstanceLock) { |
| 521 |
mActivityChoserModelPolicy = listener; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public ResolveInfo getDefaultActivity() { |
| 535 |
synchronized (mInstanceLock) { |
| 536 |
ensureConsistentState(); |
| 537 |
if (!mActivities.isEmpty()) { |
| 538 |
return mActivities.get(0).resolveInfo; |
| 539 |
} |
| 540 |
} |
| 541 |
return null; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
public void setDefaultActivity(int index) { |
| 555 |
synchronized (mInstanceLock) { |
| 556 |
ensureConsistentState(); |
| 557 |
|
| 558 |
ActivityResolveInfo newDefaultActivity = mActivities.get(index); |
| 559 |
ActivityResolveInfo oldDefaultActivity = mActivities.get(0); |
| 560 |
|
| 561 |
final float weight; |
| 562 |
if (oldDefaultActivity != null) { |
| 563 |
|
| 564 |
weight = oldDefaultActivity.weight - newDefaultActivity.weight |
| 565 |
+ DEFAULT_ACTIVITY_INFLATION; |
| 566 |
} else { |
| 567 |
weight = DEFAULT_HISTORICAL_RECORD_WEIGHT; |
| 568 |
} |
| 569 |
|
| 570 |
ComponentName defaultName = new ComponentName( |
| 571 |
newDefaultActivity.resolveInfo.activityInfo.packageName, |
| 572 |
newDefaultActivity.resolveInfo.activityInfo.name); |
| 573 |
HistoricalRecord historicalRecord = new HistoricalRecord(defaultName, |
| 574 |
System.currentTimeMillis(), weight); |
| 575 |
addHisoricalRecord(historicalRecord); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
private void persistHistoricalDataIfNeeded() { |
| 589 |
if (!mReadShareHistoryCalled) { |
| 590 |
throw new IllegalStateException("No preceding call to #readHistoricalData"); |
| 591 |
} |
| 592 |
if (!mHistoricalRecordsChanged) { |
| 593 |
return; |
| 594 |
} |
| 595 |
mHistoricalRecordsChanged = false; |
| 596 |
if (!TextUtils.isEmpty(mHistoryFileName)) { |
| 597 |
new PersistHistoryAsyncTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, |
| 598 |
new ArrayList<HistoricalRecord>(mHistoricalRecords), mHistoryFileName); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
public void setActivitySorter(ActivitySorter activitySorter) { |
| 610 |
synchronized (mInstanceLock) { |
| 611 |
if (mActivitySorter == activitySorter) { |
| 612 |
return; |
| 613 |
} |
| 614 |
mActivitySorter = activitySorter; |
| 615 |
if (sortActivitiesIfNeeded()) { |
| 616 |
notifyChanged(); |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public void setHistoryMaxSize(int historyMaxSize) { |
| 635 |
synchronized (mInstanceLock) { |
| 636 |
if (mHistoryMaxSize == historyMaxSize) { |
| 637 |
return; |
| 638 |
} |
| 639 |
mHistoryMaxSize = historyMaxSize; |
| 640 |
pruneExcessiveHistoricalRecordsIfNeeded(); |
| 641 |
if (sortActivitiesIfNeeded()) { |
| 642 |
notifyChanged(); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public int getHistoryMaxSize() { |
| 653 |
synchronized (mInstanceLock) { |
| 654 |
return mHistoryMaxSize; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public int getHistorySize() { |
| 664 |
synchronized (mInstanceLock) { |
| 665 |
ensureConsistentState(); |
| 666 |
return mHistoricalRecords.size(); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
@Override |
| 671 |
protected void finalize() throws Throwable { |
| 672 |
super.finalize(); |
| 673 |
mPackageMonitor.unregister(); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
private void ensureConsistentState() { |
| 683 |
boolean stateChanged = loadActivitiesIfNeeded(); |
| 684 |
stateChanged |= readHistoricalDataIfNeeded(); |
| 685 |
pruneExcessiveHistoricalRecordsIfNeeded(); |
| 686 |
if (stateChanged) { |
| 687 |
sortActivitiesIfNeeded(); |
| 688 |
notifyChanged(); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
private boolean sortActivitiesIfNeeded() { |
| 700 |
if (mActivitySorter != null && mIntent != null |
| 701 |
&& !mActivities.isEmpty() && !mHistoricalRecords.isEmpty()) { |
| 702 |
mActivitySorter.sort(mIntent, mActivities, |
| 703 |
Collections.unmodifiableList(mHistoricalRecords)); |
| 704 |
return true; |
| 705 |
} |
| 706 |
return false; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
private boolean loadActivitiesIfNeeded() { |
| 716 |
if (mReloadActivities && mIntent != null) { |
| 717 |
mReloadActivities = false; |
| 718 |
mActivities.clear(); |
| 719 |
List<ResolveInfo> resolveInfos = mContext.getPackageManager() |
| 720 |
.queryIntentActivities(mIntent, 0); |
| 721 |
final int resolveInfoCount = resolveInfos.size(); |
| 722 |
for (int i = 0; i < resolveInfoCount; i++) { |
| 723 |
ResolveInfo resolveInfo = resolveInfos.get(i); |
| 724 |
ActivityInfo activityInfo = resolveInfo.activityInfo; |
| 725 |
if (ActivityManager.checkComponentPermission(activityInfo.permission, |
| 726 |
android.os.Process.myUid(), activityInfo.applicationInfo.uid, |
| 727 |
activityInfo.exported) == PackageManager.PERMISSION_GRANTED) { |
| 728 |
mActivities.add(new ActivityResolveInfo(resolveInfo)); |
| 729 |
} |
| 730 |
} |
| 731 |
return true; |
| 732 |
} |
| 733 |
return false; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
private boolean readHistoricalDataIfNeeded() { |
| 744 |
if (mCanReadHistoricalData && mHistoricalRecordsChanged && |
| 745 |
!TextUtils.isEmpty(mHistoryFileName)) { |
| 746 |
mCanReadHistoricalData = false; |
| 747 |
mReadShareHistoryCalled = true; |
| 748 |
readHistoricalDataImpl(); |
| 749 |
return true; |
| 750 |
} |
| 751 |
return false; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
private boolean addHisoricalRecord(HistoricalRecord historicalRecord) { |
| 761 |
final boolean added = mHistoricalRecords.add(historicalRecord); |
| 762 |
if (added) { |
| 763 |
mHistoricalRecordsChanged = true; |
| 764 |
pruneExcessiveHistoricalRecordsIfNeeded(); |
| 765 |
persistHistoricalDataIfNeeded(); |
| 766 |
sortActivitiesIfNeeded(); |
| 767 |
notifyChanged(); |
| 768 |
} |
| 769 |
return added; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
private void pruneExcessiveHistoricalRecordsIfNeeded() { |
| 776 |
final int pruneCount = mHistoricalRecords.size() - mHistoryMaxSize; |
| 777 |
if (pruneCount <= 0) { |
| 778 |
return; |
| 779 |
} |
| 780 |
mHistoricalRecordsChanged = true; |
| 781 |
for (int i = 0; i < pruneCount; i++) { |
| 782 |
HistoricalRecord prunedRecord = mHistoricalRecords.remove(0); |
| 783 |
if (DEBUG) { |
| 784 |
Log.i(LOG_TAG, "Pruned: " + prunedRecord); |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
public final static class HistoricalRecord { |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public final ComponentName activity; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
public final long time; |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
public final float weight; |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
public HistoricalRecord(String activityName, long time, float weight) { |
| 817 |
this(ComponentName.unflattenFromString(activityName), time, weight); |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
public HistoricalRecord(ComponentName activityName, long time, float weight) { |
| 828 |
this.activity = activityName; |
| 829 |
this.time = time; |
| 830 |
this.weight = weight; |
| 831 |
} |
| 832 |
|
| 833 |
@Override |
| 834 |
public int hashCode() { |
| 835 |
final int prime = 31; |
| 836 |
int result = 1; |
| 837 |
result = prime * result + ((activity == null) ? 0 : activity.hashCode()); |
| 838 |
result = prime * result + (int) (time ^ (time >>> 32)); |
| 839 |
result = prime * result + Float.floatToIntBits(weight); |
| 840 |
return result; |
| 841 |
} |
| 842 |
|
| 843 |
@Override |
| 844 |
public boolean equals(@Nullable Object obj) { |
| 845 |
if (this == obj) { |
| 846 |
return true; |
| 847 |
} |
| 848 |
if (obj == null) { |
| 849 |
return false; |
| 850 |
} |
| 851 |
if (getClass() != obj.getClass()) { |
| 852 |
return false; |
| 853 |
} |
| 854 |
HistoricalRecord other = (HistoricalRecord) obj; |
| 855 |
if (activity == null) { |
| 856 |
if (other.activity != null) { |
| 857 |
return false; |
| 858 |
} |
| 859 |
} else if (!activity.equals(other.activity)) { |
| 860 |
return false; |
| 861 |
} |
| 862 |
if (time != other.time) { |
| 863 |
return false; |
| 864 |
} |
| 865 |
if (Float.floatToIntBits(weight) != Float.floatToIntBits(other.weight)) { |
| 866 |
return false; |
| 867 |
} |
| 868 |
return true; |
| 869 |
} |
| 870 |
|
| 871 |
@Override |
| 872 |
public String toString() { |
| 873 |
StringBuilder builder = new StringBuilder(); |
| 874 |
builder.append("["); |
| 875 |
builder.append("; activity:").append(activity); |
| 876 |
builder.append("; time:").append(time); |
| 877 |
builder.append("; weight:").append(new BigDecimal(weight)); |
| 878 |
builder.append("]"); |
| 879 |
return builder.toString(); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
public final class ActivityResolveInfo implements Comparable<ActivityResolveInfo> { |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
public final ResolveInfo resolveInfo; |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
public float weight; |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public ActivityResolveInfo(ResolveInfo resolveInfo) { |
| 904 |
this.resolveInfo = resolveInfo; |
| 905 |
} |
| 906 |
|
| 907 |
@Override |
| 908 |
public int hashCode() { |
| 909 |
return 31 + Float.floatToIntBits(weight); |
| 910 |
} |
| 911 |
|
| 912 |
@Override |
| 913 |
public boolean equals(@Nullable Object obj) { |
| 914 |
if (this == obj) { |
| 915 |
return true; |
| 916 |
} |
| 917 |
if (obj == null) { |
| 918 |
return false; |
| 919 |
} |
| 920 |
if (getClass() != obj.getClass()) { |
| 921 |
return false; |
| 922 |
} |
| 923 |
ActivityResolveInfo other = (ActivityResolveInfo) obj; |
| 924 |
if (Float.floatToIntBits(weight) != Float.floatToIntBits(other.weight)) { |
| 925 |
return false; |
| 926 |
} |
| 927 |
return true; |
| 928 |
} |
| 929 |
|
| 930 |
public int compareTo(ActivityResolveInfo another) { |
| 931 |
return Float.floatToIntBits(another.weight) - Float.floatToIntBits(weight); |
| 932 |
} |
| 933 |
|
| 934 |
@Override |
| 935 |
public String toString() { |
| 936 |
StringBuilder builder = new StringBuilder(); |
| 937 |
builder.append("["); |
| 938 |
builder.append("resolveInfo:").append(resolveInfo.toString()); |
| 939 |
builder.append("; weight:").append(new BigDecimal(weight)); |
| 940 |
builder.append("]"); |
| 941 |
return builder.toString(); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
private final class DefaultSorter implements ActivitySorter { |
| 949 |
private static final float WEIGHT_DECAY_COEFFICIENT = 0.95f; |
| 950 |
|
| 951 |
private final Map<ComponentName, ActivityResolveInfo> mPackageNameToActivityMap = |
| 952 |
new HashMap<ComponentName, ActivityResolveInfo>(); |
| 953 |
|
| 954 |
public void sort(Intent intent, List<ActivityResolveInfo> activities, |
| 955 |
List<HistoricalRecord> historicalRecords) { |
| 956 |
Map<ComponentName, ActivityResolveInfo> componentNameToActivityMap = |
| 957 |
mPackageNameToActivityMap; |
| 958 |
componentNameToActivityMap.clear(); |
| 959 |
|
| 960 |
final int activityCount = activities.size(); |
| 961 |
for (int i = 0; i < activityCount; i++) { |
| 962 |
ActivityResolveInfo activity = activities.get(i); |
| 963 |
activity.weight = 0.0f; |
| 964 |
ComponentName componentName = new ComponentName( |
| 965 |
activity.resolveInfo.activityInfo.packageName, |
| 966 |
activity.resolveInfo.activityInfo.name); |
| 967 |
componentNameToActivityMap.put(componentName, activity); |
| 968 |
} |
| 969 |
|
| 970 |
final int lastShareIndex = historicalRecords.size() - 1; |
| 971 |
float nextRecordWeight = 1; |
| 972 |
for (int i = lastShareIndex; i >= 0; i--) { |
| 973 |
HistoricalRecord historicalRecord = historicalRecords.get(i); |
| 974 |
ComponentName componentName = historicalRecord.activity; |
| 975 |
ActivityResolveInfo activity = componentNameToActivityMap.get(componentName); |
| 976 |
if (activity != null) { |
| 977 |
activity.weight += historicalRecord.weight * nextRecordWeight; |
| 978 |
nextRecordWeight = nextRecordWeight * WEIGHT_DECAY_COEFFICIENT; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
Collections.sort(activities); |
| 983 |
|
| 984 |
if (DEBUG) { |
| 985 |
for (int i = 0; i < activityCount; i++) { |
| 986 |
Log.i(LOG_TAG, "Sorted: " + activities.get(i)); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
private void readHistoricalDataImpl() { |
| 993 |
FileInputStream fis = null; |
| 994 |
try { |
| 995 |
fis = mContext.openFileInput(mHistoryFileName); |
| 996 |
} catch (FileNotFoundException fnfe) { |
| 997 |
if (DEBUG) { |
| 998 |
Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName); |
| 999 |
} |
| 1000 |
return; |
| 1001 |
} |
| 1002 |
try { |
| 1003 |
XmlPullParser parser = Xml.newPullParser(); |
| 1004 |
parser.setInput(fis, StandardCharsets.UTF_8.name()); |
| 1005 |
|
| 1006 |
int type = XmlPullParser.START_DOCUMENT; |
| 1007 |
while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) { |
| 1008 |
type = parser.next(); |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (!TAG_HISTORICAL_RECORDS.equals(parser.getName())) { |
| 1012 |
throw new XmlPullParserException("Share records file does not start with " |
| 1013 |
+ TAG_HISTORICAL_RECORDS + " tag."); |
| 1014 |
} |
| 1015 |
|
| 1016 |
List<HistoricalRecord> historicalRecords = mHistoricalRecords; |
| 1017 |
historicalRecords.clear(); |
| 1018 |
|
| 1019 |
while (true) { |
| 1020 |
type = parser.next(); |
| 1021 |
if (type == XmlPullParser.END_DOCUMENT) { |
| 1022 |
break; |
| 1023 |
} |
| 1024 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1025 |
continue; |
| 1026 |
} |
| 1027 |
String nodeName = parser.getName(); |
| 1028 |
if (!TAG_HISTORICAL_RECORD.equals(nodeName)) { |
| 1029 |
throw new XmlPullParserException("Share records file not well-formed."); |
| 1030 |
} |
| 1031 |
|
| 1032 |
String activity = parser.getAttributeValue(null, ATTRIBUTE_ACTIVITY); |
| 1033 |
final long time = |
| 1034 |
Long.parseLong(parser.getAttributeValue(null, ATTRIBUTE_TIME)); |
| 1035 |
final float weight = |
| 1036 |
Float.parseFloat(parser.getAttributeValue(null, ATTRIBUTE_WEIGHT)); |
| 1037 |
HistoricalRecord readRecord = new HistoricalRecord(activity, time, weight); |
| 1038 |
historicalRecords.add(readRecord); |
| 1039 |
|
| 1040 |
if (DEBUG) { |
| 1041 |
Log.i(LOG_TAG, "Read " + readRecord.toString()); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (DEBUG) { |
| 1046 |
Log.i(LOG_TAG, "Read " + historicalRecords.size() + " historical records."); |
| 1047 |
} |
| 1048 |
} catch (XmlPullParserException xppe) { |
| 1049 |
Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, xppe); |
| 1050 |
} catch (IOException ioe) { |
| 1051 |
Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, ioe); |
| 1052 |
} finally { |
| 1053 |
if (fis != null) { |
| 1054 |
try { |
| 1055 |
fis.close(); |
| 1056 |
} catch (IOException ioe) { |
| 1057 |
|
| 1058 |
} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
private final class PersistHistoryAsyncTask extends AsyncTask<Object, Void, Void> { |
| 1067 |
|
| 1068 |
@Override |
| 1069 |
@SuppressWarnings("unchecked") |
| 1070 |
public Void doInBackground(Object... args) { |
| 1071 |
List<HistoricalRecord> historicalRecords = (List<HistoricalRecord>) args[0]; |
| 1072 |
String hostoryFileName = (String) args[1]; |
| 1073 |
|
| 1074 |
FileOutputStream fos = null; |
| 1075 |
|
| 1076 |
try { |
| 1077 |
fos = mContext.openFileOutput(hostoryFileName, Context.MODE_PRIVATE); |
| 1078 |
} catch (FileNotFoundException fnfe) { |
| 1079 |
Log.e(LOG_TAG, "Error writing historical recrod file: " + hostoryFileName, fnfe); |
| 1080 |
return null; |
| 1081 |
} |
| 1082 |
|
| 1083 |
XmlSerializer serializer = Xml.newSerializer(); |
| 1084 |
|
| 1085 |
try { |
| 1086 |
serializer.setOutput(fos, null); |
| 1087 |
serializer.startDocument(StandardCharsets.UTF_8.name(), true); |
| 1088 |
serializer.startTag(null, TAG_HISTORICAL_RECORDS); |
| 1089 |
|
| 1090 |
final int recordCount = historicalRecords.size(); |
| 1091 |
for (int i = 0; i < recordCount; i++) { |
| 1092 |
HistoricalRecord record = historicalRecords.remove(0); |
| 1093 |
serializer.startTag(null, TAG_HISTORICAL_RECORD); |
| 1094 |
serializer.attribute(null, ATTRIBUTE_ACTIVITY, |
| 1095 |
record.activity.flattenToString()); |
| 1096 |
serializer.attribute(null, ATTRIBUTE_TIME, String.valueOf(record.time)); |
| 1097 |
serializer.attribute(null, ATTRIBUTE_WEIGHT, String.valueOf(record.weight)); |
| 1098 |
serializer.endTag(null, TAG_HISTORICAL_RECORD); |
| 1099 |
if (DEBUG) { |
| 1100 |
Log.i(LOG_TAG, "Wrote " + record.toString()); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
serializer.endTag(null, TAG_HISTORICAL_RECORDS); |
| 1105 |
serializer.endDocument(); |
| 1106 |
|
| 1107 |
if (DEBUG) { |
| 1108 |
Log.i(LOG_TAG, "Wrote " + recordCount + " historical records."); |
| 1109 |
} |
| 1110 |
} catch (IllegalArgumentException iae) { |
| 1111 |
Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, iae); |
| 1112 |
} catch (IllegalStateException ise) { |
| 1113 |
Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, ise); |
| 1114 |
} catch (IOException ioe) { |
| 1115 |
Log.e(LOG_TAG, "Error writing historical recrod file: " + mHistoryFileName, ioe); |
| 1116 |
} finally { |
| 1117 |
mCanReadHistoricalData = true; |
| 1118 |
if (fos != null) { |
| 1119 |
try { |
| 1120 |
fos.close(); |
| 1121 |
} catch (IOException e) { |
| 1122 |
|
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
return null; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
private final class DataModelPackageMonitor extends PackageMonitor { |
| 1134 |
|
| 1135 |
@Override |
| 1136 |
public void onSomePackagesChanged() { |
| 1137 |
mReloadActivities = true; |
| 1138 |
} |
| 1139 |
} |
| 1140 |
} |