Changeset c4ebc73 for apps


Ignore:
Timestamp:
Jun 2, 2019 11:08:38 AM (10 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
aa0bafb
Parents:
471c5d4
Message:

TCG: Add new getInstance() method for Android
so instance with stale context is not returned.
Return null in Android for old method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/TunnelControllerGroup.java

    r471c5d4 rc4ebc73  
    7777    /**
    7878     *  In I2PAppContext will instantiate if necessary and always return non-null.
    79      *  As of 0.9.4, when in RouterContext, will return null (except in Android)
     79     *  As of 0.9.4, when in RouterContext, will return null
    8080     *  if the TCG has not yet been started by the router.
     81     *  As of 0.9.41, that's true for Android as well.
    8182     *
    8283     *  @throws IllegalArgumentException if unable to load from i2ptunnel.config
     
    8586        synchronized (TunnelControllerGroup.class) {
    8687            if (_instance == null) {
    87                 I2PAppContext ctx = I2PAppContext.getGlobalContext();
     88                if (!SystemVersion.isAndroid()) {
     89                    I2PAppContext ctx = I2PAppContext.getGlobalContext();
     90                    if (!ctx.isRouterContext()) {
     91                        _instance = new TunnelControllerGroup(ctx, null, null);
     92                        _instance.startup();
     93                    }
     94                } // else wait for the router to start it
     95            }
     96            return _instance;
     97        }
     98    }
     99
     100    /**
     101     *  In I2PAppContext will instantiate if necessary and always return non-null.
     102     *  When in RouterContext, will return null (except in Android)
     103     *  if the TCG has not yet been started by the router.
     104     *  In Android, if the old instance uses a stale context, it will replace it.
     105     *
     106     *  @throws IllegalArgumentException if unable to load from i2ptunnel.config
     107     *  @since 0.9.41
     108     */
     109    public static TunnelControllerGroup getInstance(I2PAppContext ctx) {
     110        synchronized (TunnelControllerGroup.class) {
     111            if (_instance == null) {
    88112                if (SystemVersion.isAndroid() || !ctx.isRouterContext()) {
    89113                    _instance = new TunnelControllerGroup(ctx, null, null);
    90                     if (!SystemVersion.isAndroid())
    91                         _instance.startup();
     114                    _instance.startup();
    92115                } // else wait for the router to start it
     116            } else {
     117                if (SystemVersion.isAndroid() && _instance._context != ctx) {
     118                    ctx.logManager().getLog(TunnelControllerGroup.class).warn("Old context in TCG");
     119                    _instance.shutdown();
     120                    _instance = new TunnelControllerGroup(ctx, null, null);
     121                 }
    93122            }
    94123            return _instance;
     
    121150        _sessions = new HashMap<I2PSession, Set<TunnelController>>(4);
    122151        synchronized (TunnelControllerGroup.class) {
    123             if (_instance == null)
     152            if (_instance == null) {
    124153                _instance = this;
    125         }
    126         if (_instance != this) {
    127             _log.logAlways(Log.WARN, "New TunnelControllerGroup, now you have two");
    128             if (_log.shouldLog(Log.WARN))
    129                 _log.warn("I did it", new Exception());
     154            } else {
     155                _log.logAlways(Log.WARN, "New TunnelControllerGroup, now you have two");
     156                if (_log.shouldLog(Log.WARN))
     157                    _log.warn("I did it", new Exception());
     158            }
    130159        }
    131160        _state = INITIALIZED;
Note: See TracChangeset for help on using the changeset viewer.