添加fragment支持

pull/9/head
AriaLyy 8 years ago
parent 47993ba297
commit 173a9c50f2
  1. 2
      .idea/misc.xml
  2. 4
      Aria/build.gradle
  3. 64
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  4. 1
      app/src/main/AndroidManifest.xml
  5. 15
      app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java
  6. 13
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  7. 125
      app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java
  8. 17
      app/src/main/res/layout/activity_fragment.xml
  9. 9
      app/src/main/res/layout/activity_main.xml
  10. 12
      app/src/main/res/layout/fragment_download.xml

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 84
versionName "2.3.6"
versionCode 85
versionName "2.3.7"
}
buildTypes {
release {

@ -24,9 +24,7 @@ import android.app.DialogFragment;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.view.ContextThemeWrapper;
import android.widget.PopupWindow;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.task.Task;
@ -51,43 +49,43 @@ import com.arialyy.aria.core.task.Task;
/**
* 预处理完成
*/
public static final String ACTION_PRE = "ACTION_PRE";
public static final String ACTION_PRE = "ACTION_PRE";
/**
* 下载开始前事件
*/
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
/**
* 开始下载事件
*/
public static final String ACTION_START = "ACTION_START";
public static final String ACTION_START = "ACTION_START";
/**
* 恢复下载事件
*/
public static final String ACTION_RESUME = "ACTION_RESUME";
public static final String ACTION_RESUME = "ACTION_RESUME";
/**
* 正在下载事件
*/
public static final String ACTION_RUNNING = "ACTION_RUNNING";
public static final String ACTION_RUNNING = "ACTION_RUNNING";
/**
* 停止下载事件
*/
public static final String ACTION_STOP = "ACTION_STOP";
public static final String ACTION_STOP = "ACTION_STOP";
/**
* 取消下载事件
*/
public static final String ACTION_CANCEL = "ACTION_CANCEL";
public static final String ACTION_CANCEL = "ACTION_CANCEL";
/**
* 下载完成事件
*/
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
/**
* 下载失败事件
*/
public static final String ACTION_FAIL = "ACTION_FAIL";
public static final String ACTION_FAIL = "ACTION_FAIL";
/**
* 下载实体
*/
public static final String ENTITY = "DOWNLOAD_ENTITY";
public static final String ENTITY = "DOWNLOAD_ENTITY";
/**
* 位置
*/
@ -95,7 +93,7 @@ import com.arialyy.aria.core.task.Task;
/**
* 速度
*/
public static final String CURRENT_SPEED = "CURRENT_SPEED";
public static final String CURRENT_SPEED = "CURRENT_SPEED";
private Aria() {
}
@ -118,7 +116,17 @@ import com.arialyy.aria.core.task.Task;
/**
* 处理Fragment
*/
private static AMReceiver whit(Fragment fragment) {
public static AMReceiver whit(Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity()).get(fragment);
}
/**
* 处理Fragment
*/
public static AMReceiver whit(android.support.v4.app.Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
@ -151,10 +159,6 @@ import com.arialyy.aria.core.task.Task;
return AriaManager.getInstance(dialog.getContext()).get(dialog);
}
private static void checkNull(Object obj) {
if (obj == null) throw new IllegalArgumentException("不能传入空对象");
}
/**
* 处理通用事件
*/
@ -185,6 +189,30 @@ import com.arialyy.aria.core.task.Task;
return AriaManager.getInstance(popupWindow.getContentView().getContext());
}
/**
* 处理Fragment的通用任务
*/
public static AriaManager get(Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity());
}
/**
* 处理Fragment的通用任务
*/
public static AriaManager get(android.support.v4.app.Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
: fragment.getActivity());
}
private static void checkNull(Object obj) {
if (obj == null) throw new IllegalArgumentException("不能传入空对象");
}
public static class SimpleSchedulerListener implements OnSchedulerListener {
@Override public void onTaskPre(Task task) {

@ -26,6 +26,7 @@
<activity android:name=".activity.SingleTaskActivity"/>
<activity android:name=".activity.MultiTaskActivity"/>
<activity android:name=".activity.FragmentActivity"/>
</application>
</manifest>

@ -0,0 +1,15 @@
package com.arialyy.simple.activity;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.FragmentDownloadBinding;
/**
* Created by Aria.Lao on 2017/1/4.
*/
public class FragmentActivity extends BaseActivity<FragmentDownloadBinding> {
@Override protected int setLayoutId() {
return R.layout.activity_fragment;
}
}

@ -38,11 +38,11 @@ import com.arialyy.simple.pop.DownloadPopupWindow;
* Created by Lyy on 2016/10/13.
*/
public class MainActivity extends BaseActivity<ActivityMainBinding> {
@Bind(R.id.toolbar) Toolbar mBar;
@Bind(R.id.single_task) Button mSigleBt;
@Bind(R.id.multi_task) Button mMultiBt;
@Bind(R.id.dialog_task) Button mDialogBt;
@Bind(R.id.pop_task) Button mPopBt;
@Bind(R.id.toolbar) Toolbar mBar;
@Bind(R.id.single_task) Button mSigleBt;
@Bind(R.id.multi_task) Button mMultiBt;
@Bind(R.id.dialog_task) Button mDialogBt;
@Bind(R.id.pop_task) Button mPopBt;
@Override protected int setLayoutId() {
return R.layout.activity_main;
@ -99,6 +99,9 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
//pop.showAsDropDown(mRootView);
pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0);
break;
case R.id.fragment_task:
startActivity(new Intent(this, FragmentActivity.class));
break;
}
}
}

@ -0,0 +1,125 @@
package com.arialyy.simple.fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.core.AbsFragment;
import com.arialyy.simple.R;
import com.arialyy.simple.databinding.FragmentDownloadBinding;
import com.arialyy.simple.dialog.DownloadDialog;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
/**
* Created by Aria.Lao on 2017/1/4.
*/
public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
@Override protected void init(Bundle savedInstanceState) {
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState();
setBtState(state != DownloadEntity.STATE_DOWNLOAD_ING);
} else {
setBtState(true);
}
}
@Override public void onResume() {
super.onResume();
Aria.whit(this).addSchedulerListener(new DownloadFragment.MyDialogDownloadCallback());
}
@OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.whit(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadName("daialog.apk")
.start();
break;
case R.id.stop:
Aria.whit(this).load(DOWNLOAD_URL).stop();
break;
case R.id.cancel:
Aria.whit(this).load(DOWNLOAD_URL).cancel();
break;
}
}
@Override protected void onDelayLoad() {
}
@Override protected int setLayoutId() {
return R.layout.fragment_download;
}
@Override protected void dataCallback(int result, Object obj) {
}
private void setBtState(boolean startEnable) {
mStart.setEnabled(startEnable);
mCancel.setEnabled(!startEnable);
mStop.setEnabled(!startEnable);
}
private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener {
@Override public void onTaskPre(Task task) {
super.onTaskPre(task);
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
setBtState(false);
}
@Override public void onTaskStop(Task task) {
super.onTaskStop(task);
setBtState(true);
mSpeed.setText("0.0kb/s");
}
@Override public void onTaskCancel(Task task) {
super.onTaskCancel(task);
setBtState(true);
mPb.setProgress(0);
mSpeed.setText("0.0kb/s");
}
@Override public void onTaskRunning(Task task) {
super.onTaskRunning(task);
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
}
}
}

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment
android:id="@+id/fragment1"
android:name="com.arialyy.simple.fragment.DownloadFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</layout>

@ -47,5 +47,14 @@
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/fragment_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="在Fragment中使用"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</layout>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/dialog_download"/>
</LinearLayout>
</layout>
Loading…
Cancel
Save