Android Open Source - Genius-Android Command Service






From Project

Back to project page Genius-Android.

License

The source code is released under:

Apache License

If you think the Android project Genius-Android 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

/*
 * Copyright (C) 2014 Qiujuer <qiujuer@live.cn>
 * WebSite http://www.qiujuer.net//from w ww . j  ava2 s. c  om
 * Created 09/17/2014
 * Changed 01/13/2015
 * Version 1.0.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.qiujuer.genius.command;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import net.qiujuer.genius.util.Tools;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by QiuJu
 * on 2014/9/17.
 */
public class CommandService extends Service {
    private CommandServiceImpl mImpl;

    @Override
    public void onCreate() {
        super.onCreate();
        mImpl = new CommandServiceImpl();
    }

    @Override
    public IBinder onBind(Intent intent) {
        if (mImpl == null)
            mImpl = new CommandServiceImpl();
        return mImpl;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        super.onUnbind(intent);
        stopSelf();
        return false;
    }

    @Override
    public void onDestroy() {
        if (mImpl != null) {
            mImpl.destroy();
            mImpl = null;
        }
        super.onDestroy();
        // Kill process
        android.os.Process.killProcess(android.os.Process.myPid());
    }


    private class CommandServiceImpl extends ICommandInterface.Stub {
        private Map<String, CommandExecutor> mCommandExecutorMap = new HashMap<String, CommandExecutor>();
        private Lock mMapLock = new ReentrantLock();
        private Thread mTimeoutThread;

        public CommandServiceImpl() {
            // Init
            mTimeoutThread = new Thread(CommandServiceImpl.class.getName()) {
                @Override
                public void run() {
                    // When thread is not destroy
                    while (mTimeoutThread == this && !this.isInterrupted()) {
                        if (mCommandExecutorMap != null && mCommandExecutorMap.size() > 0) {
                            try {
                                mMapLock.lock();
                                Collection<CommandExecutor> commandExecutors = mCommandExecutorMap.values();
                                for (CommandExecutor executor : commandExecutors) {
                                    // Kill Service Process
                                    if (executor.isTimeOut())
                                        android.os.Process.killProcess(android.os.Process.myPid());
                                    if (mTimeoutThread != this && this.isInterrupted())
                                        break;
                                }
                            } finally {
                                mMapLock.unlock();
                            }
                        }
                        // Sleep 10 Second
                        Tools.sleepIgnoreInterrupt(10000);
                    }
                }
            };
            mTimeoutThread.setDaemon(true);
            mTimeoutThread.start();
        }

        /**
         * Destroy
         */
        protected void destroy() {
            if (mTimeoutThread != null) {
                mTimeoutThread.interrupt();
                mTimeoutThread = null;
            }
            try {
                mMapLock.lock();
                mCommandExecutorMap.clear();
                mCommandExecutorMap = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                mMapLock.unlock();
            }
        }


        /**
         * Run Command
         *
         * @param params params
         * @return result
         * @throws android.os.RemoteException
         */
        @Override
        public String command(String id, int timeout, String params) throws RemoteException {
            CommandExecutor executor = mCommandExecutorMap.get(id);
            if (executor == null) {
                try {
                    mMapLock.lock();
                    executor = mCommandExecutorMap.get(id);
                    if (executor == null) {
                        executor = CommandExecutor.create(timeout, params);
                        mCommandExecutorMap.put(id, executor);
                    }
                } finally {
                    mMapLock.unlock();
                }
            }

            // Get Result
            String result = executor.getResult();

            try {
                mMapLock.lock();
                mCommandExecutorMap.remove(id);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                mMapLock.unlock();
            }
            return result;
        }

        /**
         * Cancel command
         *
         * @param id command.id
         * @throws android.os.RemoteException
         */
        @Override
        public void cancel(String id) throws RemoteException {
            CommandExecutor executor = mCommandExecutorMap.get(id);
            if (executor != null) {
                try {
                    mMapLock.lock();
                    mCommandExecutorMap.remove(id);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    mMapLock.unlock();
                }
                executor.destroy();
            }
        }

        /**
         * Get Task Count
         *
         * @return Map Count
         * @throws android.os.RemoteException
         */
        @Override
        public int getTaskCount() throws RemoteException {
            if (mCommandExecutorMap == null)
                return 0;
            return mCommandExecutorMap.size();
        }
    }
}




Java Source Code List

net.qiujuer.genius.ApplicationTest.java
net.qiujuer.genius.Attributes.java
net.qiujuer.genius.GeniusUI.java
net.qiujuer.genius.Genius.java
net.qiujuer.genius.animation.TouchEffectAnimator.java
net.qiujuer.genius.animation.TouchEffect.java
net.qiujuer.genius.app.BlurKit.java
net.qiujuer.genius.app.BlurNative.java
net.qiujuer.genius.app.UIKitHandlerPoster.java
net.qiujuer.genius.app.UIKitSyncPost.java
net.qiujuer.genius.app.UIKit.java
net.qiujuer.genius.command.CommandExecutor.java
net.qiujuer.genius.command.CommandService.java
net.qiujuer.genius.command.Command.java
net.qiujuer.genius.nettool.DnsResolve.java
net.qiujuer.genius.nettool.NetModel.java
net.qiujuer.genius.nettool.Ping.java
net.qiujuer.genius.nettool.Telnet.java
net.qiujuer.genius.nettool.TraceRouteContainer.java
net.qiujuer.genius.nettool.TraceRouteThread.java
net.qiujuer.genius.nettool.TraceRoute.java
net.qiujuer.genius.sample.ApplicationTest.java
net.qiujuer.genius.sample.CaseActivity.java
net.qiujuer.genius.sample.MainActivity.java
net.qiujuer.genius.sample.TwoActivity.java
net.qiujuer.genius.util.FixedList.java
net.qiujuer.genius.util.GeniusException.java
net.qiujuer.genius.util.HashUtils.java
net.qiujuer.genius.util.LogWriter.java
net.qiujuer.genius.util.Log.java
net.qiujuer.genius.util.Tools.java
net.qiujuer.genius.widget.GeniusButton.java
net.qiujuer.genius.widget.GeniusCheckBox.java
net.qiujuer.genius.widget.GeniusTextView.java