Android Open Source - MaterialManager Network Service






From Project

Back to project page MaterialManager.

License

The source code is released under:

There is no license, someone decided to pretty much republish Cabinet with no credit so I?m taking away the license altogether.

If you think the Android project MaterialManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.cabinet.services;
//w w  w .  jav a2 s.co m
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import com.afollestad.cabinet.R;
import com.afollestad.cabinet.file.CloudFile;
import com.afollestad.cabinet.sftp.SftpClient;
import com.afollestad.cabinet.ui.DrawerActivity;

public class NetworkService extends Service {

    private final IBinder mBinder = new LocalBinder();
    private SftpClient mSftp;

    public class LocalBinder extends Binder {
        public NetworkService getService() {
            return NetworkService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null && intent.getAction().equals(DISCONNECT_SFTP)) {
            if (mSftp != null) {
                if (mSftp.isConnected())
                    mSftp.disconnect();
                mSftp = null;
            }
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(CONNECTION_NOTI);
            sendBroadcast(new Intent(DISCONNECT_SFTP));
        }
        return START_STICKY;
    }

    public interface SftpGetCallback {
        public abstract void onSftpClient(SftpClient client);

        public abstract void onError(Exception e);
    }

    private static final int CONNECTION_NOTI = 1000;
    private static final int OPEN_MAIN = 2000;
    private static final int DISCONNECT_RC = 3000;
    public static final String DISCONNECT_SFTP = "com.afollestad.cabinet.services.DISCONNECT_SFTP";

    private void startPersistedNotification(CloudFile file) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent mainIntent = new Intent(this, DrawerActivity.class)
                .putExtra("remote", file);
        PendingIntent mainPi = PendingIntent.getActivity(this, OPEN_MAIN,
                mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder nb = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_status)
                .setContentTitle(getString(R.string.cabinet_sftp))
                .setContentText(getString(R.string.connection_notification_content,
                        file.getRemote().getHost(), file.getRemote().getPort()))
                .setContentIntent(mainPi)
                .setOngoing(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Intent disconnectIntent = new Intent(this, NetworkService.class)
                    .setAction(DISCONNECT_SFTP);
            PendingIntent disconnectPi = PendingIntent.getService(this, DISCONNECT_RC,
                    disconnectIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            nb.addAction(R.drawable.ic_stat_close, getString(R.string.disconnect), disconnectPi);
            nm.notify(CONNECTION_NOTI, nb.build());
            return;
        }
        nm.notify(CONNECTION_NOTI, nb.getNotification());
    }

    public SftpClient getSftpClient() {
        return mSftp;
    }

    public void getSftpClient(final SftpGetCallback callback, final CloudFile from) {
        if (mSftp == null) mSftp = new SftpClient();
        else if (mSftp.isConnected()) {
            if (!mSftp.getRemote().equals(from.getRemote())) {
                mSftp.disconnect();
            } else {
                callback.onSftpClient(mSftp);
                return;
            }
        }
        mSftp.setRemote(from.getRemote())
                .connect(new SftpClient.CompletionCallback() {
                    @Override
                    public void onComplete() {
                        callback.onSftpClient(mSftp);
                        startPersistedNotification(from);
                    }

                    @Override
                    public void onError(Exception e) {
                        callback.onError(e);
                    }
                });
    }

    @Override
    public void onDestroy() {
        if (mSftp != null) {
            mSftp.disconnect();
            mSftp = null;
        }
        super.onDestroy();
    }
}




Java Source Code List

com.afollestad.cabinet.App.java
com.afollestad.cabinet.ApplicationTest.java
com.afollestad.cabinet.adapters.FileAdapter.java
com.afollestad.cabinet.adapters.NavigationDrawerAdapter.java
com.afollestad.cabinet.cab.CopyCab.java
com.afollestad.cabinet.cab.CutCab.java
com.afollestad.cabinet.cab.MainCab.java
com.afollestad.cabinet.cab.PickerCab.java
com.afollestad.cabinet.cab.base.BaseCab.java
com.afollestad.cabinet.cab.base.BaseFileCab.java
com.afollestad.cabinet.comparators.AlphabeticalComparator.java
com.afollestad.cabinet.comparators.ExtensionComparator.java
com.afollestad.cabinet.comparators.FoldersFirstComparator.java
com.afollestad.cabinet.comparators.HighLowSizeComparator.java
com.afollestad.cabinet.comparators.LastModifiedComparator.java
com.afollestad.cabinet.comparators.LowHighSizeComparator.java
com.afollestad.cabinet.file.CloudFile.java
com.afollestad.cabinet.file.LocalFile.java
com.afollestad.cabinet.file.Remote.java
com.afollestad.cabinet.file.base.FileFilter.java
com.afollestad.cabinet.file.base.File.java
com.afollestad.cabinet.file.root.LsParser.java
com.afollestad.cabinet.file.root.LsTokenizer.java
com.afollestad.cabinet.file.root.RootFile.java
com.afollestad.cabinet.fragments.AboutDialog.java
com.afollestad.cabinet.fragments.DetailsDialog.java
com.afollestad.cabinet.fragments.DirectoryFragment.java
com.afollestad.cabinet.fragments.NavigationDrawerFragment.java
com.afollestad.cabinet.fragments.RemoteConnectionDialog.java
com.afollestad.cabinet.fragments.WelcomeFragment.java
com.afollestad.cabinet.services.NetworkService.java
com.afollestad.cabinet.sftp.FileNotExistsException.java
com.afollestad.cabinet.sftp.SftpClient.java
com.afollestad.cabinet.ui.DrawerActivity.java
com.afollestad.cabinet.ui.SettingsActivity.java
com.afollestad.cabinet.ui.TextEditor.java
com.afollestad.cabinet.ui.base.NetworkedActivity.java
com.afollestad.cabinet.ui.base.ThemableActivity.java
com.afollestad.cabinet.utils.APKIconDownloader.java
com.afollestad.cabinet.utils.PauseOnScrollListener.java
com.afollestad.cabinet.utils.Perm.java
com.afollestad.cabinet.utils.Pins.java
com.afollestad.cabinet.utils.StorageHelper.java
com.afollestad.cabinet.utils.ThemeUtils.java
com.afollestad.cabinet.utils.TimeUtils.java
com.afollestad.cabinet.utils.Utils.java
com.afollestad.cabinet.zip.Unzipper.java
com.afollestad.cabinet.zip.Zipper.java
eu.chainfire.libsuperuser.ApplicationTest.java
eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.HideOverlaysReceiver.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java